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
},
};
var arr = [
{
id: 'a',
num: 1
},
{
id: 'b',
num: 1
},
{
id: 'c',
num: 1
},
{
id: 'd',
num: 1
},
{
id: 'e',
num: 1
},
{
id: 'f',
num: 1
},
{
id: 'g',
num: 1
}
];
for (var i=10000; i > 0; i--) {
for (var key in obj) {
console.log(obj[key].id);
}
}
for (var i=10000; i > 0; i--) {
Object.keys(obj).forEach(key => console.log(obj[key].id));
}
for (var i=10000; i > 0; i--) {
const values = Object.values(obj);
for (var j=0,l=values.length; j < l; j++) {
console.log(values[j].id);
}
}
for (var i=10000; i > 0; i--) {
for (var j=0,l=arr.length; j < l; j++) {
console.log(arr[j].id);
}
}
for (var i=10000; i > 0; i--) {
arr.forEach(n => console.log(n.id));
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for-in | |
Object.keys | |
Object.values | |
Array for | |
Array forEach |
Test name | Executions per second |
---|---|
for-in | 6.1 Ops/sec |
Object.keys | 5.3 Ops/sec |
Object.values | 6.0 Ops/sec |
Array for | 6.2 Ops/sec |
Array forEach | 5.3 Ops/sec |
I'll break down the provided benchmark definition and test cases, explaining what is being tested, the pros and cons of each approach, and other considerations.
Benchmark Definition:
The main goal of this benchmark is to compare the performance of four different approaches for iterating over an object or array:
for-in
(traditional JavaScript loop)Object.keys()
with a forEach
loopObject.values()
with a traditional for
loopArray.forEach()
Test Cases:
Each test case is defined as an individual benchmark, with its own script preparation code and HTML preparation code (which is empty in this case).
Here's what each test case does:
for-in
for-in
loop.id
for each iteration.Object.keys() with forEach
Object.keys()
.forEach
, logging the value of id
for each iteration.Object.values()
with traditional for
Object.values()
.for
loop, logging the value of id
for each iteration.Array.forEach()
Array.forEach()
, logging the value of id
for each iteration.Performance Comparison:
The benchmark compares the performance of these four approaches on a specific test case, which involves iterating over an object with multiple properties. The results show that:
for-in
is the fastest approach (6.234 executions per second).Object.keys()
with forEach
is slightly slower than for-in
but still relatively fast (5.996 executions per second).Object.values()
with a traditional for
loop is slower than both of the above approaches (5.349 executions per second).Array.forEach()
is also slower, likely due to the overhead of calling forEach()
on an array object (5.305 executions per second).Pros and Cons:
Here's a brief summary of the pros and cons for each approach:
for-in
:Object.keys()
with forEach
:Array.forEach()
).Object.values()
with a traditional for
loop:Array.forEach()
:forEach()
on an array object.Other Considerations:
When choosing an approach, consider the specific requirements of your use case. For example:
for-in
or Object.keys()
with forEach
might be a good choice.Array.forEach()
is likely the best option.Object.values()
with a traditional for
loop could be a good choice.Keep in mind that this benchmark only compares these four approaches under specific conditions. In general, it's essential to understand the performance characteristics of different iteration methods and choose the one that best fits your use case.