Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys MT
(version: 1)
Comparing performance of:
for-in vs Object.keys
Created:
11 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1 };
Tests:
for-in
for (var i=10000; i > 0; i--) { for (var key in obj) { console.log(key); } }
Object.keys
for (var i=10000; i > 0; i--) { for (let key of Object.keys(obj)) { console.log(key); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-in
Object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
8.0 Ops/sec
Object.keys
7.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 11 months ago):
This benchmark compares two different ways of iterating over the properties of a JavaScript object: using the `for-in` loop and using `Object.keys()` with a `for-of` loop. ### Test Cases Breakdown 1. **Test Case - for-in** - **Code**: ```javascript for (var i=10000; i > 0; i--) { for (var key in obj) { console.log(key); } } ``` - **Method**: This uses the `for-in` loop to iterate over all enumerable properties of the `obj` object. - **Pros**: - Simple syntax that allows you to directly loop over object properties. - **Cons**: - Iterates over all enumerable properties, which may include properties inherited from the object's prototype chain. This can lead to unexpected results if not handled properly. - In some cases, performance may suffer due to checking properties along the prototype chain. 2. **Test Case - Object.keys** - **Code**: ```javascript for (var i=10000; i > 0; i--) { for (let key of Object.keys(obj)) { console.log(key); } } ``` - **Method**: This uses `Object.keys(obj)` to retrieve an array of the object's own enumerable property names and iterates over them using a `for-of` loop. - **Pros**: - Only iterates over the object's own properties, avoiding any inherited properties which could lead to cleaner results. - More predictable behavior and better encapsulation of the object's own attributes. - **Cons**: - May have additional overhead of creating an array of keys first, which could potentially lead to slower performance in some scenarios compared to a straightforward `for-in` loop, especially in cases with a large number of properties. ### Benchmark Results - The benchmark results indicate the number of executions per second for each method. The `for-in` loop performed at approximately 7.99 executions per second, while the `Object.keys` method performed slightly lower at around 7.27 executions per second. ### Conclusion and Considerations - **Performance**: The results suggest that, in this particular case and environment, the `for-in` loop is marginally faster than using `Object.keys`. However, the difference in performance should be weighed against the implications of control over property enumeration and potential for inherited enumerable properties, which might not be desired for all cases. - **Usability**: When designing your code, you should consider whether you need to account for inherited properties or merely focus on the object’s own properties. If the latter is true, `Object.keys` is generally the safer and more predictable choice, despite a potential performance trade-off. - **Alternatives**: Other alternatives to consider for iterating over object properties in modern JavaScript include: - **`Object.entries(obj)`**: This returns an array of key-value pairs, which can be more convenient for certain applications. - **`Object.values(obj)`**: If only values are needed, this allows you to access values without keys. - **`forEach` method on the array returned by `Object.keys()` or `Object.entries()`**: This provides a functional approach to iteration. Ultimately, the choice between these methods will depend on specific project needs, performance requirements, and coding style preferences.
Related benchmarks:
for-in vs object.keys - 2
for-in vs object.entries
for-in vs for-of+object.keys
for-in vs for key of Object.keys
for-in vs object.keys true
for-in vs for-of keys
for-in vs for object.keys
for-in vs object.keys in loop
for-in vs for-of and object.keys
Comments
Confirm delete:
Do you really want to delete benchmark?