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

refactor(InfiniteLoader.js): replace inefficient abuse of reduce #1277

Merged
merged 2 commits into from
Dec 10, 2018

Conversation

jedwards1211
Copy link
Contributor

@jedwards1211 jedwards1211 commented Nov 28, 2018

Unless VMs specifically optimize this, doing concat inside of reduce is wasteful because it creates an array of length 1, then an array of length 2, etc. costing O(n^2) time and space. [].concat(...stuff.map(...)) does the same thing in O(n) time and space.

Thanks for contributing to react-virtualized!

Before submitting a pull request, please complete the following checklist:

  • The existing test suites (npm test) all pass
  • For any new features or bug fixes, both positive and negative test cases have been added
  • For any new features, documentation has been added
  • For any documentation changes, the text has been proofread and is clear to both experienced users and beginners.
  • Format your code with prettier (npm run prettier).
  • Run the Flow typechecks (npm run typecheck).

Here is a short checklist of additional things to keep in mind before submitting:

  • Please make sure your pull request description makes it very clear what you're trying to accomplish. If it's a bug fix, please also provide a failing test case (if possible). In either case, please add additional unit test coverage for your changes. :)
  • Be sure you have notifications setup so that you'll see my code review responses. (I may ask you to make some adjustments before merging.)

Unless VMs specifically optimize this, doing `concat` inside of `reduce` is wasteful because it creates an array of length 1, then an array of length 2, etc. costing `O(n^2)` space and time for something that should take `O(n)`.
@jedwards1211
Copy link
Contributor Author

No CI? 😱

@wuweiweiwu
Copy link
Contributor

hm. There are some CI issues with pull requests not coming from the source repo. But ill test locally

reduced.concat([unloadedRange.startIndex, unloadedRange.stopIndex]),
[],
);
const squashedUnloadedRanges = [].concat(...unloadedRanges.map(
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused why can't we just do

const squashedUnloadedRanges = unloadedRanges.map(....

Copy link
Contributor Author

@jedwards1211 jedwards1211 Dec 7, 2018

Choose a reason for hiding this comment

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

A little-known feature of concat is that it flattens top-level array arguments:

> [].concat([1,2], [3,4])
(4) [1, 2, 3, 4]

unloadedRanges.map(...) returns an array of arrays rather than a flattened array.

This is important to know for anytime you need to flatten one level and don't want to depend on any library or utility code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I see. TIL.

Copy link

@aptfx aptfx Dec 11, 2018

Choose a reason for hiding this comment

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

Depending on unlimited varargs is not a good idea. The maximum number of arguments is context and implementation dependent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is that true even when using Array.prototype.concat.apply([], unloadedRanges)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup nevermind, PR coming in a moment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants