reduce-vs-foreach

JavaScript performance comparison

Test case created by liyuanqiu

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const data = Array(10000).fill(1).map((item, index) => ({
    id: index,
    prop1: index ** Math.random(),
    prop2: index ** Math.random(),
  }));

};
</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
foreach
const target = {};
data.forEach((item) => {
  target[item.id] = item;
});
pending…
reduce
const target = data.reduce((acc, curr) => ({
  ...acc,
  [curr.id]: curr,
}), {});
pending…
reduce-with-assign
const target = data.reduce((acc, curr) => Object.assign(acc, {
  [curr.id]: curr,
}), {});
pending…
for-of
const target = {};
for (const item of data) {
  target[item.id] = item;
}
pending…
for
const target = {};
for (let i = 0; i < data.length; i += 1) {
  target[data[i].id] = data[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