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

fix(index.js): avoid creating a new object in reduce loop #14

Merged
merged 1 commit into from
Jun 13, 2017

Conversation

mikesherov
Copy link
Contributor

@mikesherov mikesherov commented Jun 12, 2017

As outlined here, avoiding parameter reassign by creating a new object occasionally can cause performance issues. This happens to be one of those cases!

This partially reverts e583186

Please see below for performance implications for an 1800 module webpack run!

screen shot 2017-06-12 at 9 17 38 am

...acc,
[usedFilepath]: usedFilepath,
}), {});
const fileDepsBy = compilation.fileDependencies.reduce((acc, usedFilepath) => {
Copy link

Choose a reason for hiding this comment

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

const fileDepsBy = new Set(compilation.fileDependencies) should theoretically be much faster. I'd be very interested to see the results!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, even this approach is now 1ms vs. ~2s

Copy link

Choose a reason for hiding this comment

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

the mutating reduce is 1ms, or the Set is 1ms?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mutating reduce. O(n) mutation isn't terrible compared to O(n^2) Object.assign! Though you might get sub milliseconds on native Set usage... haven't tried it.

Copy link

Choose a reason for hiding this comment

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

It might be worth using Set if only to avoid the eslint override :-) but it'd be great to try it and determine if it's significantly better.

Copy link

Choose a reason for hiding this comment

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

Where is an example of this new Set method you refer to?

Copy link
Owner

@tomchentw tomchentw left a comment

Choose a reason for hiding this comment

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

Great catch. Really hates the impure penalty.

@@ -26,6 +26,8 @@
// Variables
"no-use-before-define": [2, "nofunc"],
"no-unused-vars": [2, {"args": "none"}],
// Performance Considerations
"no-param-reassign": [0],
Copy link
Owner

Choose a reason for hiding this comment

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

Although I'd prefer disable this using single line comment

[usedFilepath]: usedFilepath,
}), {});
const fileDepsBy = compilation.fileDependencies.reduce((acc, usedFilepath) => {
acc[usedFilepath] = usedFilepath;
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe switching to _.set(acc, usedFilepath, usedFilepath) could just skipping the eslint error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think @ljharb's suggestion of using Set is right move long term. Just wanted fast rebuilds back first so thanks for merging!

@tomchentw tomchentw merged commit 8dcf206 into tomchentw:master Jun 13, 2017
@tomchentw
Copy link
Owner

Released v3.0.2

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.

4 participants