Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a for loop instead of forEach() in dispatch()
We don't care about holey array semantics because we manage the array ourselves
- Loading branch information
5b58608
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.
What makes this a performance optimisation?
5b58608
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.
http://jsfiddle.net/ssSt5/2/ @gajus
5b58608
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.
Wouldn't it be slightly more efficient to just store the initial
length
of thelisteners
array and only iterate up to that index (rather than copying the array)? I assume you're copying it in case a listener results in a new subscriber being added to the array- but since subscribers are added onto the end of the array... it seems like storing the initial length would be sufficient.(I think it's actually a tiny bit more performant to store
length
in a var before iterating anyway.)5b58608
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.
This is an optimization because forEach() has more complicated logic per spec to deal with sparse arrays. Also it's better to not allocate a function when we can easily avoid that.
No, we can't just limit ourselves to the last index because subscriptions can also be removed. However we do have an optimization to avoid cloning the array on every dispatch: see the next few commits.
5b58608
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.
Yeah, sorry Dan. I looked at this commit before noticing the follow-up commit. :)
5b58608
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.
As for your last point, I believe modern engines optimize array.length access in the loop anyway and hoisting that call doesn't make it faster.
5b58608
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.
@gaearon if you ping me once the optimizations are complete I can migrate them to redux-batched-subscribe.
5b58608
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.
@tappleby So far I don't think there's anything I can squeeze out of this
5b58608
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.
@tappleby You'll want to look at three recently added tests in
createStore.spec.js
as there's some behavior that is hard to get right when optimizing5b58608
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.
Not sure about that.
https://jsfiddle.net/qw1y8g3y/
test.count
is assessed on each iteration, the same way thattest.length
would.That said, I could not confirm this using JSPerf, http://jsperf.com/quick-for-vs-foreach. Maybe the implication of testing length is negligible.
5b58608
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.
V8 (and other engines too) are smarter than we give them credit for.
http://mrale.ph/blog/2014/12/24/array-length-caching.html
5b58608
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.
And more food for thought: http://mrale.ph/blog/2012/12/15/microbenchmarks-fairy-tale.html
5b58608
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.
@gaearon GMTA
5b58608
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.
This recent knowledge saved me countless of hours when fixing react-css-modules performance issue (https://github.com/gajus/react-css-modules/issues/89). Thank you. 👍
5b58608
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.
Glad to help!