var obj = {
a: 1,
b: 2,
c: 3,
d: 4,
0: 'a',
1: 'b',
str: 'long string',
nested: {
nested: 0
},
hello: 55.5
};
for (let i = 0; i < 10000; i++) {
for (let entries = Object.entries(obj), i = entries.length - 1; i >= 0; i--) {
console.log(entries[i])
}
}
for (let i = 0; i < 10000; i++) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
console.log([key, obj[key]])
}
}
}
for (let i = 0; i < 10000; i++) {
for (let keyArray = Object.keys(obj), i = keyArray.length - 1, key = keyArray[i]; i >= 0; key = keyArray[--i]) {
console.log([key, obj[key]]);
}
}
for (let i = 0; i < 10000; i++) {
for (let keyArray = Object.keys(obj), i = keyArray.length - 1, key = keyArray[i]; i >= 0; key = keyArray[--i]) {
console.log(key, obj[key]);
}
}
for (let i = 0; i < 10000; i++) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key, obj[key])
}
}
}
for (let i = 0; i < 10000; i++) {
for (let entries = Object.entries(obj), i = entries.length - 1, entry = entries[i]; i >= 0; entry = entries[i--]) {
console.log(entry[0], entry[1])
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.entries | |
for..in | |
Object.keys | |
Object.keys no entry | |
for..in no array | |
Object.entries no array |
Test name | Executions per second |
---|---|
Object.entries | 1.4 Ops/sec |
for..in | 1.6 Ops/sec |
Object.keys | 1.7 Ops/sec |
Object.keys no entry | 1.6 Ops/sec |
for..in no array | 2.2 Ops/sec |
Object.entries no array | 1.7 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The benchmark in question compares the performance of four different approaches: Object.entries
, for..in
, Object.keys
, and variations of these two.
What is tested?
The benchmark tests the performance of each approach in iterating over an object's keys and values. Specifically:
Object.entries
returns an array of key-value pairs.for..in
iterates over the object's property names (including those with non-standard names).Object.keys
returns an array of property names.The benchmark uses a single object, obj
, with 10 properties: a
, b
, c
, d
, 0
, 1
, str
, nested
, and hello
.
Options compared
The benchmark compares the following options:
Object.entries
: Returns an array of key-value pairs.for..in
: Iterates over the object's property names.__proto__
).Object.keys
: Returns an array of property names.Variations
The benchmark also tests variations of for..in
:
Object.entries no array
: Returns an array of key-value pairs instead of property names.for..in no array
: Iterates over the object's property names without returning any values.Performance
The benchmark runs each option on multiple browsers and devices, measuring the time it takes to execute a loop that iterates over the object's keys and values using each approach.
Conclusion
In general, Object.entries
is the fastest option when iterating over an object's key-value pairs. However, if you only need to iterate over property names, for..in
or Object.keys
might be faster due to their more efficient iteration mechanisms.
Keep in mind that this benchmark only tests a specific use case and may not represent all scenarios. Additionally, the results may vary depending on the specific object, browser, and device being used.