var obj = {};
for (i = 0; i < 1000; ++i) {
obj[i] = i;
}
Object.keys(obj).forEach(
(key) => key,
);
Object.values(obj).forEach(
(value) => value,
);
Object.entries(obj).forEach(
(entry) => entry,
);
Object.entries(obj).forEach(
([key, value]) => [key, value],
);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
object with keys | |
object with values | |
object with entries 1 | |
object with entries 2 |
Test name | Executions per second |
---|---|
object with keys | 82553.4 Ops/sec |
object with values | 427565.7 Ops/sec |
object with entries 1 | 22376.0 Ops/sec |
object with entries 2 | 20803.4 Ops/sec |
I'll provide an explanation of the benchmark and its test cases.
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark created on MeasureThat.net. The benchmark compares the performance of three different approaches to iterate over object keys, values, or entries using the forEach
method.
Options Compared
There are four test cases:
keys()
method to get an array of a given object's own enumerable property names and then iterates over this array using forEach
.values()
method to get an array of a given object's own enumerable property values and then iterates over this array using forEach
.entries()
method to get an array of a given object's own enumerable key-value pairs and then iterates over this array using forEach
.[key, value]
) to extract the key-value pair from each entry.Pros and Cons of Each Approach
keys()
method array.keys()
or values()
.entries()
method array.Other Considerations
Libraries Used
None of the provided test cases use any external libraries. They rely solely on built-in JavaScript methods and syntax.
Special JS Features/Syntax
There are no special JavaScript features or syntax mentioned in the benchmark. All tests are written using standard JavaScript syntax.
I hope this explanation helps you understand the benchmark and its test cases!