Performance of Array vs. Object (v267)

Revision 267 of this benchmark created on


Setup

const arr = [];
const obj = {};
const N = 10000;
  for(let i = 0; i < N; i++) {
      var o = {payload:i};
      arr.push(o);
      obj[i] = o;
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Array Performance
let sum = 0;
for (let x=0; x<arr.length; ++x) {
    sum += arr[x].payload
}
ready
Using Object.values() for of
let sum = 0;
for (const x of Object.values(obj)) {
    sum += x.payload;
}
ready
Object Performance using known length
let sum = 0;
for (let x=0; x<N; ++x) {
    sum += obj[x].payload
}
ready
Using Object.keys()
let sum = 0;
const keys = Object.keys(obj);
for (let x=0; x<keys.length; ++x) {
    sum += obj[keys[x]].payload
}
ready
Using Object.values()
let sum = 0;
const values = Object.values(obj);
for (let x=0; x<values.length; ++x) {
    sum += values[x].payload
}
ready
Using Object.keys() for of
let sum = 0;
for (const key of Object.keys(obj)) {
    sum += obj[key].payload
}
ready
Array Performance for of
let sum = 0;
for (const x of arr) {
    sum += x
}
ready
Array reduce
const sum = arr.reduce((r, x) => r + x, 0)
ready
Object using integer keys for of
let sum = 0;
const keys = Object.keys(obj).map(Number)
for (const x of keys) {
    sum += obj[x].payload
}
ready
Object using integer keys for of
let sum = 0;
const keys = Object.keys(obj).map(Number)
for (let i = 0; i < keys.length; i++) {
    sum += obj[keys[i]].payload
}
ready

Revisions

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