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

'for' loop corrected for better performances #9

Merged
merged 1 commit into from
Feb 8, 2012
Merged

'for' loop corrected for better performances #9

merged 1 commit into from
Feb 8, 2012

Conversation

bfontaine
Copy link
Contributor

It's better for performances to write the following:
for (var i=0, len=foo.length; i<len; i++) { /*...*/ }
than
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
Copy link
Owner

tommoor commented Feb 8, 2012

Makes sense, thanks.

tommoor pushed a commit that referenced this pull request Feb 8, 2012
'for' loop corrected to increase performance
@tommoor tommoor merged commit d64dfa3 into tommoor:master Feb 8, 2012
@rslinckx
Copy link

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
Copy link
Contributor Author

Yes, sorry I didn't see that.

@tommoor tommoor mentioned this pull request Feb 9, 2012
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