Transforming large array to key value map

JavaScript performance comparison

Test case created by WadePeterson

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const input = Array.from(Array(1000)).map((_, i) => {
    const key = `key${i}`
    const value = `value${i}`
    return { key, value }
  })

};
</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
Reduce, directly mutate accumulator
const output = input.reduce((acc, { key, value }) => {
  acc[key] = value;
  return acc;
}, {})
pending…
Reduce, mutating Object.assign
const output = input.reduce((acc, { key, value }) => {
  return Object.assign(acc, { [key]: value })
}, {})
pending…
forEach
const output = {}
input.forEach(({ key, value }) => {
  output[key] = value;
})
pending…
standard for, no block scope vars
const output = {}
for(let i=0; i<input.length; i++) {
  output[input[i].key] = input[i].value;
}
pending…
for ... of
const output = {}
for(const { key, value } of input) {
  output[key] = value;
}
pending…
standard for with block scope vars
const output = {}
for(let i=0; i<input.length; i++) {
  const { key, value } = input[i]
  output[key] = value
}
pending…
pure reduce, no mutation
const output = input.reduce((acc, { key, value }) => {
  return { ...acc, [key]: value };
}, {})
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