Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys (no hof in 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) { 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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
2.0 Ops/sec
Object.keys
1.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark described on MeasureThat.net evaluates the performance differences between two approaches for iterating over the properties of an object in JavaScript: the `for-in` loop and the `Object.keys` method with a `for-of` loop. ### Benchmark Overview 1. **Test Cases:** - **`for-in` Loop:** ```javascript for (var i = 10000; i > 0; i--) { for (var key in obj) { console.log(key); } } ``` - **`Object.keys` with `for-of`:** ```javascript for (var i = 10000; i > 0; i--) { for (var key of Object.keys(obj)) console.log(key); } ``` ### Comparison of Approaches - **`for-in` Loop:** - **Pros:** - Simpler syntax for directly iterating over object properties. - Enumerates all properties, including those in the prototype chain if not filtered. - **Cons:** - Can potentially iterate over inherited properties, leading to unexpected behavior if the object is extended or modified. - Performance may vary based on the number of properties and the complexity of the prototype chain. - **`Object.keys` with `for-of`:** - **Pros:** - Only returns the object's own properties (not inherited), making it safer to use when you want to avoid prototype interference. - Generally performs better in modern JavaScript engines, particularly for medium to large-sized objects. - **Cons:** - Requires an additional method call (`Object.keys`), adding potential overhead, although most engines optimize this. ### Results Interpretation From the provided benchmark results: - The `for-in` loop executed approximately **2.04 executions per second**. - The `Object.keys` method executed approximately **1.89 executions per second**. These results suggest that, in this particular environment and scenario, the `for-in` loop performed better than `Object.keys`. However, performance can vary significantly based on context and browser optimizations. ### Other Considerations 1. **Performance Variations:** - The performance observed may depend on numerous factors, including the JavaScript engine in use, the current execution context, and even the specific hardware or system state. - It's essential to conduct benchmarks in the targeted environments where your code will run to obtain meaningful results. 2. **Alternatives:** - **`forEach` Loop:** This approach, with an array of the keys via `Object.keys(obj).forEach(...)`, provides a more functional programming style. However, it may be slower due to the overhead of function calls. - **`for...in` along with `hasOwnProperty`:** To avoid inherited properties, one can pair `for-in` with `hasOwnProperty` checks. - **ES6 Features:** The `Object.entries()` and `Object.values()` methods can be alternatives for accessing both keys and values or just values, respectively, using similar loops. ### Conclusion In summary, the benchmark compares two common methods for iterating over property keys in JavaScript. While both approaches have their advantages and disadvantages, the choice of which to use often depends on the specific requirements of the task, including considerations for safety and performance. Software engineers should choose based on the context of their usage, conducting performance tests in their actual execution environments for the best insights.
Related benchmarks:
for-in vs object.keys - 2
for-in vs object.entries
for-in vs object.keybepb
for-in vs object.keys for-of
for-in vs for key of Object.keys
for-in vs object.keys with for loop fixed
for-in vs for-of 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?