Loops

JavaScript performance comparison

Revision 274 of this test case created by Romul81

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const arr = [...Array(10000).keys()]
  const res = new Array(10000)
  
  function foo(x, i) {
   res[i] = x
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Reverse for loop
for (let i = arr.length; i--;) {
 res[i] = arr[i]
}
pending…
Reverse do … while loop
let i = arr.length

do {
 res[i] = arr[i]
} while (i--)
pending…
Reverse while loop without implicit ToBoolean
let i = arr.length

while (i-- > 0) {
 res[i] = arr[i]
}
pending…
Cool guy loop
for (let i = -1; ++i < arr.length;) {
 res[i] = arr[i]
}
pending…
Cool guy loop, caching the length
for (let i = -1, len = arr.length; ++i < len;) {
 res[i] = arr[i]
}
pending…
Native Array#forEach implementation with named function
arr.forEach(foo)
pending…
Reverse while loop
let i = arr.length

while (i--) {
 res[i] = arr[i]
}
pending…
Old ’n’ busted for loop
for (let i = 0; i < arr.length; ++i) {
 res[i] = arr[i]
}
pending…
while loop that imitates a for loop
let i = 0

while (i < arr.length) {
 res[i] = arr[i]
 i++
}
pending…
Old ’n’ busted for loop, caching the length
for (let i = 0, len = arr.length; i < len; ++i) {
 res[i] = arr[i]
}
pending…
Native Array#forEach implementation
arr.forEach((x, i) => res[i] = x)
pending…
while loop that imitates a for loop, caching the length
let i = 0
const len = arr.length

while (i < len) {
 res[i] = arr[i]
 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.

0 Comments