Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For in vs Object.entries 2
(version: 0)
Comparing performance of:
For in vs Object.entries
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {} for(i=0; i<100000; i++){ obj['key_'+i] = Math.random(); }
Tests:
For in
for(let key in obj){ console.log(`${key}: ${obj[key]}`); }
Object.entries
for (const [key, value] of Object.entries(obj)) { console.log(`${key}: ${value}`); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For in
Object.entries
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For in
3.9 Ops/sec
Object.entries
3.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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:** 1. **For in**: This is a traditional loop that iterates over an object's properties using the `in` operator. It checks if a property exists in the object and then logs its value. 2. **Object.entries**: This is a more modern approach that uses the `entries()` method to iterate over an object's key-value pairs as arrays. **Pros and Cons:** * **For in**: * Pros: * Well-supported across browsers (although some older versions may have issues). * Easy to understand for those familiar with traditional loops. * Cons: * Can be slower due to the need to check if a property exists before iterating over it. * May not work well with sparse objects or objects that don't support `in`. * **Object.entries**: * Pros: * Faster than traditional loops since it avoids the overhead of checking for existing properties. * More modern and widely adopted, making it a better choice for new projects. * Cons: * Requires newer browsers that support `entries()` (specifically, from Chrome 42 onwards). * May be less familiar to developers who aren't used to this syntax. **Other considerations:** When using either approach, it's worth noting that you should also consider the following: * **Object initialization**: In both cases, the object is initialized with a fixed size of 100,000 properties. This can affect performance since a large number of objects need to be created. * **Property values**: The property values are randomly generated using `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!
Related benchmarks:
Object.entries vs Object.keys vs for...in
Object.entries VS Object.keys with obj[key]
For in vs Object.entries
For in vs Object.entries Check
Comments
Confirm delete:
Do you really want to delete benchmark?