Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For in vs Object.entries
(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<10000; 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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 YaBrowser/24.12.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For in
38.1 Ops/sec
Object.entries
34.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared options, pros and cons, library usage, special JavaScript features, and alternative approaches. **Benchmark Overview** The provided benchmark measures the performance of two loops that iterate over an object: `for...in` and `Object.entries()`. The benchmark creates a large object with 10,000 properties (key-value pairs) using a script preparation code. Then, it defines two test cases: 1. `For in`: Iterates over the object's properties using the `for...in` loop. 2. `Object.entries()`: Iterates over the object's key-value pairs using the `Object.entries()` method. **Comparison of Loop Approaches** The benchmark compares the performance of these two loops under different scenarios: * **Loop iterations**: Both loops iterate 10,000 times, which simulates a large dataset. * **Access pattern**: The `For in` loop accesses each property directly (e.g., `obj[key]`) using bracket notation. In contrast, the `Object.entries()` loop iterates over an array of key-value pairs, where each iteration accesses both the key and value. **Pros and Cons** **For...in Loop:** Pros: * Simple and intuitive syntax. * Can be used when you need to iterate over object properties in a specific order (e.g., iterating over keys, then values). Cons: * Performance is slower due to the overhead of parsing the `for...in` loop and iterating over each property manually. * May have performance issues if the object is large or has many properties. **Object.entries() Loop:** Pros: * Faster performance since it leverages optimized array iteration and reduces manual property access overhead. * Can handle large datasets efficiently, as it iterates over an array of key-value pairs. Cons: * Requires a modern JavaScript engine that supports `Object.entries()` (introduced in ECMAScript 2015). * May require additional memory allocation for the temporary array created by `Object.entries()`. * Syntax can be less familiar to developers without experience with object iteration methods. **Library Usage** There is no library explicitly mentioned in this benchmark. However, it's worth noting that `Object.entries()` is a built-in method in modern JavaScript engines (since ECMAScript 2015). If you needed to support older browsers or environments, you might use libraries like Lodash (`_.entries()`) or the `forEach` method. **Special JavaScript Features** * **For...in Loop**: This loop uses a non-standard syntax and may have performance issues due to manual property access. While it's not considered "special," its syntax can be unfamiliar to developers without experience with object iteration methods. * **Object.entries() Method**: This method is a built-in feature introduced in ECMAScript 2015, which requires support for modern JavaScript engines. **Alternative Approaches** If you wanted to test other loop approaches or variations, some alternatives could include: 1. Using a `for` loop with bracket notation (`obj[key]`) instead of `For...in`. 2. Using an array-based approach with indexing (e.g., `let i = 0; while (i < obj.length) { ... }`). 3. Utilizing modern JavaScript features like `Array.prototype.map()` or `Array.prototype.forEach()`, which can simplify object iteration. Feel free to ask if you'd like me to elaborate on any of these points!
Related benchmarks:
keys vs values
Object.entries VS Object.keys with obj[key]
For in vs Object.entries 2
For in vs Object.entries Check
Comments
Confirm delete:
Do you really want to delete benchmark?