Loop comparison

JavaScript performance comparison

Test case created by timseverien

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function fract(n) {
    return n - Math.floor(n);
  }
  
  let i, length, value;
  let sum = 0;
  
  const values = Array.from({ length: 1024 }, (_, n) => fract(Math.sin(n * 12.9898) * 43758.5453123));

};
</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
for
for (i = 0; i < values.length; i++) {
  sum += values[i];
}
pending…
for with cached length
for (i = 0, length = values.length; i < length; i++) {
  sum += values[i];
}
pending…
forEach
values.forEach((value) => {
  sum += value;
});
pending…
reduce
sum = values.reduce((result, value) => {
  return result + value;
}, 0);
pending…
for...of
for (const v of values) {
  sum += v;
}
pending…
for...of without const
for (value of values) {
  sum += value;
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments