Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

'for' loop corrected for better performances #9

merged 1 commit into from Feb 8, 2012


None yet
3 participants

bfontaine commented Feb 8, 2012

It's better for performances to write the following:
for (var i=0, len=foo.length; i<len; i++) { /*...*/ }
for (var i=0; i<foo.length; i++) { /*...*/ }
We're caching the length of the array in a variable, so Javascript won't have to re-calculate the length every time.


tommoor commented Feb 8, 2012

Makes sense, thanks.

@tommoor tommoor pushed a commit that referenced this pull request Feb 8, 2012

Tom Moor Merge pull request #9 from bfontaine/master
'for' loop corrected to increase performance

@tommoor tommoor merged commit d64dfa3 into tommoor:master Feb 8, 2012

rslinckx commented Feb 8, 2012

This breaks for the second case where you are removing the tag from while iterating over the link collection.
The length being calculated only once at the beginning will cause the loop to go too far if a link gets removed from head.
Fix is simple: use the code before the patch


bfontaine commented Feb 8, 2012

Yes, sorry I didn't see that.

@tommoor tommoor referenced this pull request Feb 9, 2012


performance #10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment