for 循环 和 Array.reduce() 对比

JavaScript performance comparison

Test case created by zhangxiangliang

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const numbers = [...Array(10000).keys()];

};
</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 循环
function sum(arr) {
    let count = 0;
    for (let i = 0; i < arr.length; i++) {
        count += arr[i]
    }
    return count;
}

sum(numbers);
pending…
Array.reduce()
numbers.reduce((count, i) => count + i);
pending…

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

Compare results of other browsers

0 Comments