Created
November 11, 2012 18:26
-
-
Save jornki/4055789 to your computer and use it in GitHub Desktop.
JavaScript loop performance
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 i = 500000, ar = [], startTime, duration, output = {}; | |
while(i--) { | |
ar.push(i); | |
} | |
// jQuery | |
startTime = new Date().getTime(); | |
$.each(ar, function(index, item) { | |
// | |
}); | |
duration = new Date().getTime() - startTime; | |
output.jquery = duration; | |
// Underscore | |
startTime = new Date().getTime(); | |
_.each(ar, function(item, index) { | |
// | |
}); | |
duration = new Date().getTime() - startTime; | |
output.undescore = duration; | |
//----- | |
// ECS5 | |
startTime = new Date().getTime(); | |
ar.forEach(function(item, index) { | |
// | |
}); | |
duration = new Date().getTime() - startTime; | |
output.foreach = duration; | |
//---- | |
//For | |
startTime = new Date().getTime(); | |
var len = ar.length; | |
for(i=0;i<len;i++) { | |
// | |
} | |
duration = new Date().getTime() - startTime; | |
output.for_regular = duration; | |
//---- | |
//For rev | |
startTime = new Date().getTime(); | |
i = ar.length; | |
for(i = ar.length; i > 0; i--) { | |
// | |
} | |
duration = new Date().getTime() - startTime; | |
output.for_reverse = duration; | |
//---- | |
//While | |
startTime = new Date().getTime(); | |
i = ar.length; | |
while(i--) { | |
// | |
} | |
duration = new Date().getTime() - startTime; | |
output.while_reverse = duration; | |
//---- | |
for(var key in output) { | |
$('body').append('<p>' + key + ': ' + output[key].toString() + ' ms</p>'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment