Skip to content

Conversation

@zbjornson
Copy link

Building arrays via arr[i] = x is ~45% faster than arr.push(x). See http://jsperf.com/sylvesterloops/2
Store references to mOrV.elements.
Inline loops instead of call out to map, which calls to forEach.

Building arrays via `arr[i] = x` is faster than `arr.push(x)`. Store
references to morv.elements.
Copy link

Choose a reason for hiding this comment

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

@zbjornson You know the length already in a lot these cases, you should use new Array(n) and then set the indexs x[i] = <val>

Copy link
Author

Choose a reason for hiding this comment

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

@megawac that actually seems detrimental in some browsers and only a minor improvement in others: http://jsperf.com/construct-array-of-fixed-size/2

Copy link
Author

Choose a reason for hiding this comment

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

(Specifically, I optimized for FF and chrome, and the gain in FF doesn't outweigh the loss in chrome.)

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.

2 participants