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

Core: Use Array#flat where supported #4459

Closed
wants to merge 14 commits into from

Conversation

aelafifi
Copy link
Contributor

@aelafifi aelafifi commented Aug 19, 2019

Summary

Solve $.map() issue when working with large arrays on Chrome browser (#4320)

Checklist

@jsf-clabot
Copy link

jsf-clabot commented Aug 19, 2019

CLA assistant check
All committers have signed the CLA.

@aelafifi
Copy link
Contributor Author

I've used the native .flat() function wherever it's supported.
Otherwise, .concat.apply() will be called, because after trying implementing a custom logic to flat the array, it takes a very long time to handle it in the browser, and as mentioned @ Won't Fix:

  • Serious performance implications of fixing a relatively rare problem

Other solution will be good (from the functional side) is to put the flatting logic into the main loop in $.map() itself, but I think it's a bad design, especially in this case since it's very rare!

src/var/flat.js Outdated Show resolved Hide resolved
@mgol
Copy link
Member

mgol commented Aug 19, 2019

Thanks for the PR! See my comment & please sign our CLA.

src/var/flat.js Outdated Show resolved Hide resolved
To avoid massive indentation changes when migrating to ES modules.
src/var/flat.js Outdated Show resolved Hide resolved
src/var/flat.js Outdated Show resolved Hide resolved
@mgol
Copy link
Member

mgol commented Aug 19, 2019

Also, it'd be great if the PR title & commit messages followed the format from https://contribute.jquery.org/commits-and-pull-requests/#commit-guidelines

We have a pre-commit hook that enforces that & it should catch those transgressions, do you have Node.js installed locally?

@aelafifi
Copy link
Contributor Author

Also, it'd be great if the PR title & commit messages followed the format from https://contribute.jquery.org/commits-and-pull-requests/#commit-guidelines

Thanks, I'll check it.

We have a pre-commit hook that enforces that & it should catch those transgressions, do you have Node.js installed locally?

Yes, I've Node.js v12.6.0

@mgol
Copy link
Member

mgol commented Aug 19, 2019

@aelafifi Can you make sure all usages of the concat.apply( [], something ) pattern are converted? I see at least one other usage in manipulation.

src/core.js Outdated Show resolved Hide resolved
Ahmed S. El-Afifi and others added 4 commits August 19, 2019 14:38
Copy link
Member

@mgol mgol left a comment

Choose a reason for hiding this comment

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

Size increase at this point: +21 bytes. Looks fine to me, I added one comment in tests.

test/unit/core.js Outdated Show resolved Hide resolved
@mgol mgol changed the title Support native array .flat() function instead of .concat.apply() Selector: Use Array#flat where supported Aug 19, 2019
src/var/flat.js Outdated Show resolved Hide resolved
@mgol
Copy link
Member

mgol commented Aug 19, 2019

I ran some benchmarks: flat is the fastest in Chrome, Firefox & Safari: https://jsperf.com/flat-vs-concat-vs-iteration

@aelafifi
Copy link
Contributor Author

I'll work on them as soon as possible,
I have a question about the PR name, why it's related to component Selector?

@timmywil
Copy link
Member

@aelafifi You're right that it's more universal than selector, but the main use for this historically has been to array-ify the result of selections.

@mgol mgol changed the title Selector: Use Array#flat where supported Core: Use Array#flat where supported Aug 19, 2019
@mgol
Copy link
Member

mgol commented Aug 19, 2019

@aelafifi Sorry, my mistake, it should have been Core as it's used in other places as well, e.g. manipulation.

@mgol
Copy link
Member

mgol commented Aug 22, 2019

I'll remove the "Needs review" label for now as the PR needs some further work.

@mgol mgol removed the Needs review label Aug 22, 2019
@aelafifi
Copy link
Contributor Author

Sorry for being late.
I've updated the points above, please check them and tell me if there is any thing wrong.

Copy link
Member

@timmywil timmywil left a comment

Choose a reason for hiding this comment

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

Seems good. Can we get rid of the concat var file?

@aelafifi
Copy link
Contributor Author

It still used from the flat var file as a secondary option if Array#flat isn't exists

@timmywil
Copy link
Member

That’s not what I meant. We’re not using concat anywhere else so it doesn’t need to be its own var file.

@aelafifi
Copy link
Contributor Author

Great, it's done.

Copy link
Member

@mgol mgol left a comment

Choose a reason for hiding this comment

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

One comment to add, otherwise looks good to me.

test/unit/core.js Show resolved Hide resolved
Copy link
Member

@timmywil timmywil left a comment

Choose a reason for hiding this comment

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

LGTM

@mgol mgol removed the Needs review label Sep 24, 2019
@mgol mgol modified the milestones: 4.0.0, 3.5.0 Sep 24, 2019
@mgol mgol closed this in 9df4f1d Sep 25, 2019
mgol pushed a commit to mgol/jquery that referenced this pull request Sep 25, 2019
Calling `Array.prototype.concat.apply( [], inputArray )` to flatten `inputArray`
crashes for large arrays; using `Array.prototype.flat` avoids these issues in
browsers that support it. In case it's necessary to support these large arrays
even in older browsers, a polyfill for `Array.prototype.flat` can be loaded.
This is already being done by many applications.

(cherry picked from 9df4f1d)

Fixes jquerygh-4320
Closes jquerygh-4459
@mgol
Copy link
Member

mgol commented Sep 25, 2019

Landed on master (9df4f1d) & 3.x-stable (2f666c1), changes will be in jQuery 3.5.0. Thanks!

@lock lock bot locked as resolved and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging this pull request may close these issues.

None yet

4 participants