var object = {};
for (let i = 0; i < 1000; i += 1) {
object['something' + i] = 'value';
}
Object.entries(object).forEach(([key, value]) => {
console.log(key, value);
});
for (const [key, value] of Object.entries(object)) {
console.log(key, value);
}
for (let i = 0; i < Object.keys(object); i += 1) {
console.log(Object.keys(object)[i], value);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
forEach | |
for of | |
traditional for loop |
Test name | Executions per second |
---|---|
forEach | 247.4 Ops/sec |
for of | 219.9 Ops/sec |
traditional for loop | 13384.1 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to measure the performance of three different approaches for iterating over an object in JavaScript:
forEach
for...of
loopsfor
loops with indexing (Object.keys()
)Script Preparation Code
The script preparation code creates a large object with 1000 properties, each assigned a value.
var object = {};
for (let i = 0; i < 1000; i += 1) {
object['something' + i] = 'value';
}
This object is used as the test data for all three iteration approaches.
Html Preparation Code
The html preparation code is empty, which means that the benchmark is running in a Node.js environment and does not have access to any browser-specific APIs.
Individual Test Cases
Each test case defines a specific benchmarking scenario:
forEach
Object.entries()
method to iterate over the object's key-value pairs.console.log
.for of
Object.entries()
method to iterate over the object's key-value pairs, just like the forEach
example.console.log
, but uses a different syntax (const [key, value] of Object.entries(object)
).traditional for loop
Object.keys()
method to get an array of the object's keys.for
loop, logging each key-value pair to the console using console.log
.Benchmark Results
The benchmark results show the number of executions per second for each test case on a specific browser (Chrome 107) and device platform (Desktop, Windows). The top performer is the traditional for
loop approach.
Pros and Cons of Each Approach
forEach
: Pros:Object.entries()
and executing a callback function for each iteration.for...of
loops: Pros:forEach
in some cases, since it avoids the overhead of a callback function.for
loops: Pros:Library Used
The Object.entries()
method is used in all test cases to iterate over the object's key-value pairs. This is a built-in JavaScript method that returns an array of tuples, where each tuple contains a key-value pair from the original object.
Special JS Features/Syntax
None are explicitly mentioned or used in this benchmark.
Alternatives
Other alternatives for iterating over objects in JavaScript include:
for...in
loops: Iterates over object's properties using their property names as keys.Object.keys()
and Object.values()
: Returns arrays of an object's property names and values, respectively.In summary, the benchmark is designed to measure the performance of three different approaches for iterating over objects in JavaScript, while also highlighting their strengths, weaknesses, and use cases.