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--) {
let sum = 0;
for (let key in obj) {
sum += obj[key].num;
}
}
for (var i=10000; i > 0; i--) {
let sum = 0;
for (let key of Object.keys(obj)) {
sum += obj[key].num;
}
}
for (var i=10000; i > 0; i--) {
let sum = 0;
for (let value of Object.values(obj)) {
sum += value.num;
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for-in | |
Object.keys | |
Object.values |
Test name | Executions per second |
---|---|
for-in | 265.0 Ops/sec |
Object.keys | 203.8 Ops/sec |
Object.values | 691.0 Ops/sec |
What is being tested?
The provided benchmark test compares the performance of three different approaches to iterate over an object's properties in JavaScript:
for-in
: This approach uses the in
operator to iterate over the object's property names.Object.keys()
: This approach uses the Object.keys()
method to get an array of the object's property names, and then iterates over this array using a for...of
loop.Object.values()
: This approach uses the Object.values()
method to get an array of the object's property values, and then iterates over this array using a for...of
loop.Options compared
The benchmark compares the performance of these three approaches on large objects with multiple properties.
Pros and Cons of each approach:
for-in
: This approach is simple and widely supported, but it can be slower than other approaches because it iterates over all property names, including inherited ones. Additionally, it doesn't provide a way to specify the iteration order.Object.keys()
: This approach provides more control over the iteration order and allows for more efficient memory usage because it only iterates over the own property names of the object, excluding inherited properties. However, it requires an additional method call (Object.keys()
) which may incur a small overhead.Object.values()
: Similar to Object.keys()
, this approach provides more control over the iteration order and allows for efficient memory usage by only iterating over the property values. However, it's only available in modern browsers that support ECMAScript 2019 (ES10).Library used
None of the benchmark tests use any external libraries.
Special JS features or syntax
None of the test cases use special JavaScript features or syntax. They all use standard JavaScript language and syntax.
Other alternatives
In addition to these three approaches, other alternatives for iterating over an object's properties include:
for...in
with hasOwnProperty()
: This approach uses the hasOwnProperty()
method to filter out inherited properties.Map
or Set
data structure: These data structures provide efficient iteration over their key-value pairs, but they are not typically used for iterating over objects.Benchmark preparation code
The benchmark preparation code creates an object with multiple properties ('a'
, 'b'
, ..., 'g'
) and sets each property to have a value of 1
. The code is generated in the "Script Preparation Code" section of the benchmark definition JSON.