var obj = {}
for(i=0; i<100000; i++){
obj['key_'+i] = Math.random();
}
for(let key in obj){
console.log(`${key}: ${obj[key]}`);
}
for (const [key, value] of Object.entries(obj)) {
console.log(`${key}: ${value}`);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
For in | |
Object.entries |
Test name | Executions per second |
---|---|
For in | 3.9 Ops/sec |
Object.entries | 3.9 Ops/sec |
I'll explain the benchmark and its options.
What is being tested?
MeasureThat.net provides a platform for users to create and run JavaScript microbenchmarks. In this case, we have two test cases: "For in" and "Object.entries". The benchmark tests how fast each approach can iterate over an object's properties.
Options compared:
in
operator. It checks if a property exists in the object and then logs its value.entries()
method to iterate over an object's key-value pairs as arrays.Pros and Cons:
in
.entries()
(specifically, from Chrome 42 onwards).Other considerations:
When using either approach, it's worth noting that you should also consider the following:
Math.random()
. While this ensures that each test case runs in isolation, it may not reflect real-world scenarios where property values might have more complex behaviors.Library usage:
There is no library being used in either of these benchmarks. However, if you were to include a library, popular ones for testing JavaScript performance would typically be benchmark
, js-benchmark
, or micro-benchmark
.
Special JS features/syntax:
Neither test case uses any special JavaScript features or syntax beyond the standard language itself. The focus is on comparing the performance of two iteration methods.
Now you know what's being tested and compared in this benchmark, as well as their pros and cons. If you have any questions about these concepts or would like further clarification, feel free to ask!