How to loop

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var targetArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    var targetArrLength = targetArr.length;
};
</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
for 0~9 without length cache
var newarr = [];
for (var i = 0; i < targetArr.length; i++) {
  newarr.push(targetArr[i]);
}
pending…
for 0~9
var newarr = [];
for (var i = 0; i < targetArrLength; i++) {
  newarr.push(targetArr[i]);
}
pending…
for 9~0
var newarr = [];
for (var i = targetArrLength - 1; i > -1; i--) {
  newarr.push(targetArr[i]);
}
pending…
while 0~9
var i = 0;
var newarr = [];
while (i != targetArrLength) {
  newarr.push(targetArr[i]);
  i++;
}
pending…
while 9~0
var i = targetArrLength - 1;
var newarr = [];
while (i != -1) {
  newarr.push(targetArr[i]);
  i--;
}
pending…
for i in arr
var newarr = [];
for (var i in targetArr) {
  newarr.push(targetArr[i]);
}
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