iterate object (v22)

Revision 22 of this benchmark created on


Preparation HTML

<script>
const proto = {
  0: 0,
  1: 1,
  2: 2,
  3: 3,
  4: 4,
  k: 5,
  l: 6,
  m: 7,
  n: 8,
  o: 9
}
const obj = {
  __proto__: proto,
  a: 0,
  b: 1,
  c: 2,
  d: 3,
  e: 4,
  f: 5,
  g: 6,
  h: 7,
  i: 8,
  j: 9
};
var sum = 0;
</script>

Setup

sum = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
Object.keys with traditional loop
var keys = Object.keys(obj);
for (let i = 0, l = keys.length; i < l; i+=1) {
  sum += obj[keys[i]];
}
ready
for..in with Object.hasOwn
for (const key in obj) {
  if (!Object.hasOwn(obj, key)) return
  sum += obj[key];
}
ready
Object.keys with for..of
for (const key of Object.keys(obj)) {
  sum += obj[key];
}
ready
Object.values with for..of
for (const value of Object.values(obj)) {
  sum += value;
}
ready
Object.entries with for..of
for (const [key, value] of Object.entries(obj)) {
  sum += value;
}
ready
for..in with obj.hasOwnProperty
for (const key in obj) {
  if (!obj.hasOwnProperty(key)) return
  sum += obj[key];
}
ready

Revisions

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