Skip to content

Instantly share code, notes, and snippets.

@devdazed
Created November 4, 2011 14:46
Show Gist options
  • Save devdazed/1339487 to your computer and use it in GitHub Desktop.
Save devdazed/1339487 to your computer and use it in GitHub Desktop.
Interesting differences in loops and incrementers
var i = 0, n = 100000000, t = 0;
console.time('i++');
for (; i < n; i++){
t += 1;
}
console.timeEnd('i++');
//i++: 147ms
i = 0; t = 0;
console.time('++i');
for (; i < n; ++i){
t += 1;
}
console.timeEnd('++i');
//++i: 183ms
i = 0; t = 0;
console.time('i+=1');
for (; i < n; i+=1){
t += 1;
}
console.timeEnd('i+=1');
//i+=1: 147ms
@devdazed
Copy link
Author

devdazed commented Nov 4, 2011

after 10,000 runs it holds consistent that all methods end up being the same at about 150ms

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