while vs for_in

JavaScript performance comparison

Revision 4 of this test case created

Info

test "while" and "for in" speed when applying to list, the array number is more than "http://jsperf.com/for-vs-for-in"

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var l1 = [2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 3, 4, 5, 6, 1, 2, 3, 4, 5, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 3, 4, 5, 6, 1, 2, 3, 4, 5, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 3, 4, 5, 6, 1, 2, 3, 4, 5, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1];
    var sum = 0;
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
test "for in"
for (var i in l1) {
  sum += l1[i];
}

 
pending…
test while
var i = 0;
while (i < l1.length) {
  sum += l1[i++];
}
pending…
test for
for(var i = 0; i < l1.length; i++) {
  sum += l1[i];
}
pending…
test for each
l1.forEach(function(l) {
  sum += l;
});
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