Skip to content

Instantly share code, notes, and snippets.

@bga
Created April 6, 2010 11:43
Show Gist options
  • Save bga/357505 to your computer and use it in GitHub Desktop.
Save bga/357505 to your computer and use it in GitHub Desktop.
// tnx http://4umi.com/web/javascript/optimize.php for idea, but while(i--) the best :) (except opera :/)
var _fn = function(){};
var i, n=30, j, m = 100000;
console.time("1");
i = n; while(i--)
{
for( j = 0; j < m; j++ ) _fn();
}
console.timeEnd("1");
console.time("2");
i = n; while(i--)
{
for( j = m; j >= 0; j-- ) _fn();
}
console.timeEnd("2");
console.time("3");
i = n; while(i--)
{
j = m; while(j--) _fn();
}
console.timeEnd("3");
console.time("4");
i = n; while(i--)
{
j = 0; do{j++; _fn();}while(j < m);
}
console.timeEnd("4");
console.time("5");
i = n; while(i--)
{
j = m - 1; do{_fn(); j--;}while(j > 0);
}
console.timeEnd("5");
console.time("6");
i = n; while(i--)
{
j = m - 1; do{_fn();}while(j--);
}
console.timeEnd("6");
console.time("7");
i = n; while(i--)
{
j = m; do{_fn();}while(--j);
}
console.timeEnd("7");
/*
ff.3,6
1: 6545ms
2: 5897ms
3: 5119ms
4: 6683ms
5: 6189ms
6: 5187ms
7: 5146ms
chrome 4 n = 9
1: 2570
2: 2039
3: 1507
4: 2464
5: 1995
6: 1512
7: 1521
opera 10.50 n = 30
1: 3934
2: 2934
3: 4215
4: 5960
5: 5165
6: 4162
7: 4170
ie n = 1
1: 1272
2: 1092
3: 1071
4: 1112
5: 1112
6: 1011
7: 992
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment