var obj = {
'a': {
id: 'a',
num: 1
},
'b': {
id: 'b',
num: 1
},
'c': {
id: 'c',
num: 1
},
'd': {
id: 'd',
num: 1
},
'e': {
id: 'e',
num: 1
},
'f': {
id: 'f',
num: 1
},
'g': {
id: 'g',
num: 1
},
};
for (var i=10000; i > 0; i--) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(obj[key].id);
}
}
}
for (var i=10000; i > 0; i--) {
for (let value of Object.values(obj)) {
console.log(value.id);
}
}
for (var i=10000; i > 0; i--) {
for (let [key, value] of Object.entries(obj)) {
console.log(value.id);
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for-in | |
Object.values | |
Object.entries |
Test name | Executions per second |
---|---|
for-in | 3.3 Ops/sec |
Object.values | 3.4 Ops/sec |
Object.entries | 3.3 Ops/sec |
Let's break down the provided JSON benchmark definition and explain what is tested, compared, pros and cons of different approaches, and other considerations.
Benchmark Definition:
The benchmark defines a test case that measures the performance difference between three different ways to iterate over an object:
for-in
: Uses the in
operator to iterate over the object's properties.Object.values
: Uses the values()
method of the Object
prototype to get an array of the object's property values.Object.entries
: Uses the entries()
method of the Object
prototype to get an array of the object's key-value pairs.Benchmark Preparation Code:
The preparation code creates a large object (obj
) with 10 properties, each containing an id
and a num
property.
var obj = {
'a': { id: 'a', num: 1 },
'b': { id: 'b', num: 1 },
'c': { id: 'c', num: 1 },
'd': { id: 'd', num: 1 },
'e': { id: 'e', num: 1 },
'f': { id: 'f', num: 1 },
'g': { id: 'g', num: 1 },
'h': { id: 'h', num: 1 },
'i': { id: 'i', num: 1 },
'j': { id: 'j', num: 1 }
};
Individual Test Cases:
There are three test cases, each with a different benchmark definition:
for-in
: Uses the in
operator to iterate over the object's properties.for (var key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(obj[key].id);
}
}
Object.values
: Uses the values()
method of the Object
prototype to get an array of the object's property values.for (let value of Object.values(obj)) {
console.log(value.id);
}
Object.entries
: Uses the entries()
method of the Object
prototype to get an array of the object's key-value pairs.for (let [key, value] of Object.entries(obj)) {
console.log(value.id);
}
Performance Comparison:
The benchmark compares the performance of each test case using a JavaScript interpreter. The results show that:
Object.values
is slightly faster than for-in
.Object.entries
is slower than both Object.values
and for-in
.Pros and Cons:
for-in
: This approach can be slower due to the additional checks for the property's existence. However, it can also provide better performance for certain use cases, such as when the object has a large number of properties.Object.values
: This approach is generally faster than for-in
because it avoids the extra checks. However, it may not be suitable for objects with many properties due to memory usage concerns.Object.entries
: This approach provides good performance but can be slower than Object.values
due to the additional overhead of creating an array of key-value pairs.Other Considerations:
Object.entries
approach creates a new array of key-value pairs, which can consume more memory. In contrast, for-in
and Object.values
do not create additional data structures.for-in
approach is generally considered less readable than the other two options due to the need for explicit checks.Alternatives:
Other alternatives to these approaches include:
Map
objects instead of regular objects, which provide faster iteration and key-value access.