Skip to content
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

Block editor: use vanilla JS Array.prototype.includes instead of Lodash #21063

Merged

Conversation

ZebulanStanphill
Copy link
Member

@ZebulanStanphill ZebulanStanphill commented Mar 21, 2020

Description

See title. In my testing, the vanilla method is slightly faster, and I find it easier to comprehend when reading the code. It also has stronger typing, making it better for pushing the codebase closer to complete type checking.

As it turns out, lodash.includes is used a lot in the code, so to make reviewing and catching mistakes easier, I'm doing one package at a time.

The long-term goal is to reduce lodash usage in cases where a standard JS function works just as well or better, which should reduce our packages' bundle sizes for 3rd parties using them. This will also increase clarity by making nullish value handling more explicit, and avoiding the use of external libraries when simple equivalents exist in standard JS.

@ZebulanStanphill ZebulanStanphill added [Type] Code Quality Issues or PRs that relate to code quality [Package] Block editor /packages/block-editor labels Mar 21, 2020
@github-actions
Copy link

github-actions bot commented Mar 21, 2020

Size Change: -24 B (0%)

Total Size: 1.13 MB

Filename Size Change
build/block-editor/index.js 109 kB -24 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.62 kB 0 B
build/api-fetch/index.js 3.4 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 7.39 kB 0 B
build/block-directory/style-rtl.css 941 B 0 B
build/block-directory/style.css 942 B 0 B
build/block-editor/style-rtl.css 10.7 kB 0 B
build/block-editor/style.css 10.7 kB 0 B
build/block-library/editor-rtl.css 7.41 kB 0 B
build/block-library/editor.css 7.41 kB 0 B
build/block-library/index.js 129 kB 0 B
build/block-library/style-rtl.css 8.04 kB 0 B
build/block-library/style.css 8.05 kB 0 B
build/block-library/theme-rtl.css 730 B 0 B
build/block-library/theme.css 732 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.2 kB 0 B
build/components/index.js 198 kB 0 B
build/components/style-rtl.css 15.9 kB 0 B
build/components/style.css 15.9 kB 0 B
build/compose/index.js 9.65 kB 0 B
build/core-data/index.js 11.4 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/data/index.js 8.44 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 569 B 0 B
build/dom/index.js 3.19 kB 0 B
build/edit-navigation/index.js 9.88 kB 0 B
build/edit-navigation/style-rtl.css 1.02 kB 0 B
build/edit-navigation/style.css 1.02 kB 0 B
build/edit-post/index.js 303 kB 0 B
build/edit-post/style-rtl.css 5.51 kB 0 B
build/edit-post/style.css 5.51 kB 0 B
build/edit-site/index.js 16.6 kB 0 B
build/edit-site/style-rtl.css 3 kB 0 B
build/edit-site/style.css 3 kB 0 B
build/edit-widgets/index.js 9.32 kB 0 B
build/edit-widgets/style-rtl.css 2.42 kB 0 B
build/edit-widgets/style.css 2.42 kB 0 B
build/editor/editor-styles-rtl.css 537 B 0 B
build/editor/editor-styles.css 539 B 0 B
build/editor/index.js 44.8 kB 0 B
build/editor/style-rtl.css 3.85 kB 0 B
build/editor/style.css 3.86 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.72 kB 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keyboard-shortcuts/index.js 2.52 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.12 kB 0 B
build/list-reusable-blocks/style-rtl.css 450 B 0 B
build/list-reusable-blocks/style.css 451 B 0 B
build/media-utils/index.js 5.29 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 788 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 14 kB 0 B
build/server-side-render/index.js 2.67 kB 0 B
build/shortcode/index.js 1.69 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.06 kB 0 B
build/viewport/index.js 1.85 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

@ZebulanStanphill
Copy link
Member Author

The test failures are because I'm using optional chaining. This PR is blocked by #19952.

@ZebulanStanphill ZebulanStanphill linked an issue Mar 21, 2020 that may be closed by this pull request
2 tasks
@ZebulanStanphill ZebulanStanphill added the [Status] Blocked Used to indicate that a current effort isn't able to move forward label Mar 21, 2020
@chrisvanpatten
Copy link
Contributor

@ZebulanStanphill Any reason it wouldn't work to land all the instances except the one which uses optional chaining? Looks like there's only one.

@ZebulanStanphill
Copy link
Member Author

@chrisvanpatten That's a good point! I've removed that instance from this PR so this can get merged sooner.

@ZebulanStanphill
Copy link
Member Author

I had to undo another instance because apparently rootChildBlocks was turning out to be undefined in the tests. I'm not sure how this can be; the type information in my editor implies it must be an array. I suspect that either there's some incorrect JSDoc type info somewhere, or else there's a bug in the unit tests, but I'm not sure.

But anyway, all tests are passing now for this PR!

@ZebulanStanphill ZebulanStanphill removed the [Status] Blocked Used to indicate that a current effort isn't able to move forward label Mar 24, 2020
@ZebulanStanphill
Copy link
Member Author

Forgot to remove the "Blocked" label until now. This PR is ready for review.

@ZebulanStanphill ZebulanStanphill force-pushed the update/block-editor-use-vanilla-array-includes branch from e55d40d to b6e547e Compare March 29, 2020 04:12
@ZebulanStanphill
Copy link
Member Author

Resolved merge conflicts.

@ZebulanStanphill ZebulanStanphill force-pushed the update/block-editor-use-vanilla-array-includes branch 2 times, most recently from 7f3b7c7 to 76585d1 Compare April 11, 2020 15:44
@ZebulanStanphill ZebulanStanphill force-pushed the update/block-editor-use-vanilla-array-includes branch from 342d758 to 089a96a Compare April 11, 2020 16:55
@ZebulanStanphill
Copy link
Member Author

Our tooling now supports optional chaining, so I brought back the changes that were using that.

@gziolo
Copy link
Member

gziolo commented Apr 16, 2020

Thanks for working on it. I like the idea in general but I have some reservations:

  • it changes the code to use native JS syntax but there is nothing that prevents to use lodash again
  • without 100% unit tests coverage and full TypeScript integration it’s difficult to ensure we don’t introduce a bug - see the cases where includes is called conditionally

Any ideas how yo encourage first the usage of native JS?

@ZebulanStanphill
Copy link
Member Author

@gziolo Regarding the discouraging of Lodash use, here are two ideas:

  • A recommendation in the contributor guidelines to prefer vanilla JS methods over Lodash equivalents.
  • An ESLint rule to warn when certain Lodash functions are used and a direct vanilla JS equivalent exists (e.g. _.includes vs. [].includes).

And yeah, I am somewhat relying on the idea that TypeScript integration will continue to improve as it has been lately. (See #18838.)

@ZebulanStanphill ZebulanStanphill force-pushed the update/block-editor-use-vanilla-array-includes branch from 089a96a to fe97247 Compare April 22, 2020 16:27
@ZebulanStanphill ZebulanStanphill force-pushed the update/block-editor-use-vanilla-array-includes branch from fe97247 to 11c3227 Compare April 22, 2020 18:50
@ZebulanStanphill ZebulanStanphill force-pushed the update/block-editor-use-vanilla-array-includes branch 3 times, most recently from e19f64a to 91658de Compare May 6, 2020 17:55
@ZebulanStanphill ZebulanStanphill force-pushed the update/block-editor-use-vanilla-array-includes branch from 91658de to 7828bfb Compare May 13, 2020 16:16
@ZebulanStanphill ZebulanStanphill changed the title Block editor: use vanilla JS Array.prototype.includes instead of Lodash. Block editor: use vanilla JS Array.prototype.includes instead of Lodash Jun 2, 2020
@ZebulanStanphill ZebulanStanphill force-pushed the update/block-editor-use-vanilla-array-includes branch 3 times, most recently from f471ba2 to 86c0095 Compare June 6, 2020 13:25
@ZebulanStanphill ZebulanStanphill force-pushed the update/block-editor-use-vanilla-array-includes branch from 86c0095 to 2d78368 Compare June 11, 2020 00:21
@ZebulanStanphill ZebulanStanphill force-pushed the update/block-editor-use-vanilla-array-includes branch from 2d78368 to d97ec7d Compare June 30, 2020 20:25
@ZebulanStanphill ZebulanStanphill force-pushed the update/block-editor-use-vanilla-array-includes branch from d97ec7d to 4e69fd8 Compare June 30, 2020 23:19
Copy link
Member

@mkaz mkaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks right, I share the same concern as @gziolo that it is not truly compatible with lodash, because if a null is passed to lodash includes() it will return false without error, but trying to use the native Array.includes() function will error if called from a null value.

It looks like the variables that might be null use the new ?. syntax that should handle it, and the majority of updates look like the variable should be defined.

Testing on the branch didn't reveal anything, but hard to test the uses cases here.

With all that, I give it a 👍

@ZebulanStanphill ZebulanStanphill merged commit 97affa3 into master Jul 2, 2020
@ZebulanStanphill ZebulanStanphill deleted the update/block-editor-use-vanilla-array-includes branch July 2, 2020 00:06
@github-actions github-actions bot added this to the Gutenberg 8.5 milestone Jul 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Block editor /packages/block-editor [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants