Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in-hasOwnProperty vs for of object.keys
(version: 1)
Comparing performance of:
for-in vs Object.keys
Created:
one year 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) { if(obj.hasOwnProperty(key)) { console.log(key); } } }
Object.keys
for (var i=10000; i > 0; i--) { for (var 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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
6.9 Ops/sec
Object.keys
6.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON compares two different approaches to iterating over the properties of an object in JavaScript: using a `for-in` loop combined with `hasOwnProperty` and using a `for-of` loop with `Object.keys`. ### Options Compared 1. **`for-in` with `hasOwnProperty`**: - **Benchmark Definition**: ```javascript for (var i=10000; i > 0; i--) { for (var key in obj) { if(obj.hasOwnProperty(key)) { console.log(key); } } } ``` - **Test Name**: "for-in" 2. **`for-of` with `Object.keys`**: - **Benchmark Definition**: ```javascript for (var i=10000; i > 0; i--) { for (var key of Object.keys(obj)) { console.log(key); } } ``` - **Test Name**: "Object.keys" ### Pros and Cons of Each Approach 1. **`for-in` with `hasOwnProperty`**: - **Pros**: - It can directly iterate over all enumerable properties of an object, including those that are added dynamically. - **Cons**: - It also iterates over properties that are inherited from the prototype chain unless filtered with `hasOwnProperty`. This can potentially lead to unexpected behavior if not handled properly. - Generally, it may also be slower compared to other methods because it checks against the prototype. 2. **`for-of` with `Object.keys`**: - **Pros**: - Cleaner and safer since `Object.keys()` returns only the object's own enumerable properties, eliminating the need for the `hasOwnProperty` check. - Generally, it can be faster because it focuses on direct properties without the need for prototype checking. - **Cons**: - Slightly less flexible in terms of directly iterating over objects' properties in other configurations (like inherited properties). ### Considerations - **Performance**: The benchmark results indicate that using `for-of` with `Object.keys` is slightly more performant (approximately 6.43 executions/second) than using `for-in` (approximately 6.92 executions/second). However, this may vary based on the JavaScript engine's optimization strategies. - **Readability and Maintainability**: The `for-of` approach is generally more readable and aligns better with modern JavaScript practices. It enhances code maintainability since it is less error-prone. ### Libraries and Features There are no specific libraries used in this benchmark; it strictly utilizes JavaScript's native capabilities. The `Object.keys()` method is a built-in JavaScript feature that creates an array of a given object's own enumerable property names, which is a useful utility for iterating over object properties. ### Alternatives 1. **`forEach` Method**: You can use `Object.keys(obj).forEach(key => { console.log(key); });` which provides a functional approach but adds overhead for function calls. 2. **`Object.entries()` or `Object.values()`**: If you need to work with values or key-value pairs instead of just keys, these methods are alternatives, e.g., `Object.entries(obj).forEach(([key, value]) => { console.log(key); });`. 3. **`for...in` (without `hasOwnProperty`)**: This would iterate through all properties of the object, including inherited ones, which can be useful if that is the required behavior. Overall, the choice among these approaches will depend on the specific use case, code readability preferences, and performance requirements.
Related benchmarks:
js for in vs object.keys
for-in with hasOwnProperty() vs object.keys
for-in with hasOwnProperty checking vs object.keys
for-in with hasOwnProperty guard vs object.keys
for-in vs for-of object.keys
for-in-hasOwnProperty vs object.keys
for-in-hasOwnProperty vs object.keys-direct
for-in own keys vs object.keys
for-in vs object.keys (no hof in Object.keys, hasOwn check)
Comments
Confirm delete:
Do you really want to delete benchmark?