Created
September 14, 2011 04:28
-
-
Save hagino3000/1215857 to your computer and use it in GitHub Desktop.
JavaScript speed check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Modern Style Spent: 862 ms | |
Primitive Style Spent: 764 ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Modern Style Spent: 896 ms | |
Primitive Style Spent: 766 ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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