Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object keys map vs object keys loop
(version: 0)
Comparing performance of:
for-in vs Object.keys map vs Object.keys loop
Created:
3 years 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 map
for (var i=10000; i > 0; i--) { Object.keys(obj).forEach(key => console.log(key)); }
Object.keys loop
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 (3)
Previous results
Fork
Test case name
Result
for-in
Object.keys map
Object.keys loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
1.4 Ops/sec
Object.keys map
1.1 Ops/sec
Object.keys loop
1.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided JSON represents a benchmark test case on the MeasureThat.net website, which compares the performance of three different approaches to iterate over an object's keys: `for-in`, `Object.keys` with `map`, and `Object.keys` with a traditional loop. **Tested Approaches** 1. **For-in**: This approach uses the `in` operator to check if a property is present in the object, and then iterates over the properties using a `for` loop. 2. **Object.keys map**: This approach uses the `Object.keys()` method to get an array of the object's keys, and then uses the `forEach()` method to iterate over the array, logging each key to the console. 3. **Object.keys loop**: This approach uses the `Object.keys()` method to get an array of the object's keys, and then uses a traditional `for` loop with a variable declaration (`var key`) to iterate over the array, logging each key to the console. **Pros and Cons** * **For-in**: + Pros: Simple and concise syntax. + Cons: Can be slower due to the overhead of checking if a property is present in the object. * **Object.keys map**: + Pros: Faster than `for-in` since it avoids the overhead of property checks. Also, uses modern JavaScript features like `map()`. + Cons: Requires modern browsers that support `map()` and can be slower for smaller arrays due to the function call overhead. * **Object.keys loop**: + Pros: Similar performance to `Object.keys map` since it avoids the overhead of property checks. Also, uses a traditional `for` loop with a variable declaration, which can be familiar to developers. + Cons: May have slightly slower performance due to the need for an explicit loop. **Library and Syntax** The benchmark test case uses no external libraries or special JavaScript features beyond what is required by each approach. The only notable feature is the use of modern JavaScript methods like `map()` in the `Object.keys map` approach, which is supported by most modern browsers. **Other Alternatives** If you were to implement this benchmark yourself, you could also consider using other approaches, such as: * Using a library like Lodash's `keys()` function * Using a more concise syntax with `for...of` loops and spread operator (`Object.keys(obj)` → `for (const key of Object.keys(obj))`) * Optimizing the code by using caching or memoization to avoid repeated property checks Keep in mind that these alternatives may not be directly comparable to the MeasureThat.net benchmark, as they would require modifications to the original test case.
Related benchmarks:
For in vs For of
function+for-in vs Object.keys
Object.entries vs Object.keys vs for...in
checks if object has any key - Object.keys vs for key in 2
Comments
Confirm delete:
Do you really want to delete benchmark?