FOR LOOPS test

JavaScript performance comparison

Revision 26 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 loop x

for (var i = 0; i < myArray.length; i++) {
  var x = myArray[i];
  if (x)
    x.aaa = 1;
}
pending…
for loop
var x;
for (var i = 0; i < myArray.length; i++) {
  if (myArray[i])
    myArray[i].aaa = 1;
}
pending…
for loop (with caching)
var x;
for (var i = 0; i < myArray.length; i++) {
    myArray[i].aaa = 1;
}
pending…
jQuery.each (for comparison)
for (var i = 0; i < myArray.length; i++) {
  var x = myArray[i];
  if (x)
    x.aaa = 1;
}
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