-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lodash: Refactor away from _.uniq()
#43330
Conversation
@@ -64,7 +64,8 @@ export const BlockSwitcherDropdownMenu = ( { clientIds, blocks } ) => { | |||
_icon = blockInformation?.icon; // Take into account active block variations. | |||
} else { | |||
const isSelectionOfSameType = | |||
uniq( blocks.map( ( { name } ) => name ) ).length === 1; | |||
[ ...new Set( blocks.map( ( { name } ) => name ) ) ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use Set.size
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, updated in ad02c59
.map( ( label ) => LABEL_TYPE_MAPPING[ label ] ) | ||
); | ||
return [ | ||
...new Set( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, is there any difference between spreading the Set and using Array.from
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! I guess the answer has a few different angles:
- Performance - they're almost equal, spreading being a bit faster: https://jsben.ch/5lKjg
- Functionality - they work the same way when working with iterables (which
Set
s are). - Transpilation - the end result will differ slightly, but that would matter if this code was transpiled (which it isn't).
Unless you have strong feelings, I'd prefer to use spread as it's way more common around the codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It isn't immediately obvious that
Set
is used to remove duplicates.
I don't know if I agree with that; [ ...new Set() ]
is a pretty common idiom these days.
Size Change: -6 B (0%) Total Size: 1.24 MB
ℹ️ View Unchanged
|
@tyxla, you are doing a great job refactoring all packages to remove |
What?
This PR removes the
_.uniq()
usage completely and deprecates the function.Why?
Lodash is known to unnecessarily inflate the bundle size of packages, and in most cases, it can be replaced with native language functionality. See these for more information and rationale:
@wordpress/api-fetch
package haslodash
as a dependency #39495How?
_.uniq( arr )
is easily replaced by native[ ...new Set( arr ) ]
.Testing Instructions
npm run other:changelog
.npm run docs:gen
.