Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object iteration for-in vs object.keys
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {}; for (var i = 10000; i > 0; i--) { obj[`key ${i}`] = i; }
Tests:
for-in
for (var key in obj) { console.log(obj[key]); }
Object.keys
Object.keys(obj).forEach(key => console.log(obj[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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
40.5 Ops/sec
Object.keys
41.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** MeasureThat.net is testing the performance of two different approaches for iterating over an object in JavaScript: `for-in` and `Object.keys()`. The benchmark definition provides a simple object with 10,000 properties, each initialized with a unique value. **Approaches Comparison** 1. **For-in loop**: This approach iterates over the object's properties using a traditional `for` loop with an array-like index (`key`). It prints the value of each property to the console. 2. **Object.keys() and forEach**: This approach uses the built-in `Object.keys()` method to get an array of the object's property names, which is then iterated over using the `forEach` method, printing the corresponding property values. **Pros and Cons** * **For-in loop**: + Pros: Simple and straightforward implementation. + Cons: May not be as efficient as other approaches due to the overhead of iterating over an array-like index. * **Object.keys() and forEach**: + Pros: More modern and efficient approach, leveraging built-in JavaScript methods. + Cons: Requires a slightly more complex setup. **Library and Special JS Features** Neither benchmark definition uses any external libraries or special JavaScript features. However, it's worth noting that `Object.keys()` is a relatively modern feature introduced in ECMAScript 2015 (ES6). **Test Case Explanation** The individual test cases are: 1. **For-in**: Iterates over the object's properties using a traditional `for` loop with an array-like index (`key`). It prints the value of each property to the console. 2. **Object.keys() and forEach**: Uses the built-in `Object.keys()` method to get an array of the object's property names, which is then iterated over using the `forEach` method, printing the corresponding property values. **Other Alternatives** Alternative approaches could include: 1. Using a library like Lodash or Underscore.js for more efficient iteration and manipulation of objects. 2. Utilizing modern JavaScript features like `for...of` loops or iterators to simplify object iteration. 3. Implementing a custom iterator function using the `Symbol.iterator` method. However, these alternatives may not be directly comparable to the `for-in` and `Object.keys()` approaches, as they would require additional setup and potentially alter the benchmark's results.
Related benchmarks:
For in vs For of
Object.entries vs Object.keys vs for...in
for-in vs object.keys vs object.values for objects perf 5
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?