Skip to content

Instantly share code, notes, and snippets.

@hagino3000
Created September 14, 2011 04:28
Show Gist options
  • Save hagino3000/1215857 to your computer and use it in GitHub Desktop.
Save hagino3000/1215857 to your computer and use it in GitHub Desktop.
JavaScript speed check
Modern Style Spent: 862 ms
Primitive Style Spent: 764 ms
Modern Style Spent: 896 ms
Primitive Style Spent: 766 ms
var seed = [], time1 = 0, time2 = 0;
for (var i=0; i<1000000; i++) { seed.push(i); }
for (var j=0; j<10; j++) {
time1 += runPrimitiveStyle();
time2 += runModernStyle();
}
console.info('Modern Style Spent:', time2, 'ms');
console.info('Primitive Style Spent:', time1, 'ms');
function runModernStyle() {
var initTime = +new Date();
var target = [], total = 0;
seed.forEach(function(s) {
target.push(s);
});
total = target.reduce(function(a, b){
return a + b;
});
console.info(total);
return +new Date() - initTime;
}
function runPrimitiveStyle() {
var initTime = +new Date();
var target = [], total = 0;
for (var i=0, len = seed.length; i < len; i++) {
target[target.length++] = seed[i];
}
for (var j=0, len = target.length; j < len; j++) {
total += target[j];
}
console.info(total);
return +new Date() - initTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment