FOR LOOPS test

JavaScript performance comparison

Revision 4 of this test case created

Info

Difference between for/in, classical for loop and classical loop with caching.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var myArray = ['a', 'b', 'c', 'd'];
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
for/in loop
var g_str = '';
for (var i in myArray) {
g_str+=myArray[i];
}
pending…
for loop (no caching)
var g_str = '';
for (var i = 0; i < myArray.length; i++) {
g_str+=myArray[i];
}
pending…
for loop (with caching)
var g_str = '';
for (var i = 0, l = myArray.length; i < l; i++) {
g_str+=myArray[i];
}
pending…
jQuery.each (for comparison)
var g_str='';
jQuery.each(myArray, function(index, elem) {
    g_str += elem;
});
pending…
es5 forEach
var g_str = '';
myArray.forEach(function(elem){
  g_str += elem;
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment