Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test forin vs keys+foreach
(version: 1)
Comparing performance of:
for in test vs keys+foreach
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
let temp = { "surname": "Verdi", "name": "Giovanni", "cfPiva": "VRDGVN90C12E567Z", "plate": "IJ789KL", "birthDate": "1990-10-20", "proviceBirth": "Torino", "cityBirth": "Torino", "proviceResidence": "Torino", "cityResidence": "Torino", "company": "LMN Srl", "mainRole": "Ingegnere", "roles": "Sviluppatore, Progettista", "injuries": "Nessuno" };let imports={};
Tests:
for in test
for (const key in temp) { imports[key] = "true"; }
keys+foreach
Object.keys(temp).forEach(key => { imports[key] = "true"; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for in test
keys+foreach
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for in test
3529745.5 Ops/sec
keys+foreach
3176668.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided JSON represents a benchmark focused on comparing two different approaches to iterate over the properties of an object in JavaScript: using a `for...in` loop and using `Object.keys()` in combination with the `forEach` method. ### Benchmark Description 1. **Test Cases**: - **For...in Loop**: ```javascript for (const key in temp) { imports[key] = "true"; } ``` This loop iterates over enumerable properties of the `temp` object. Each key is assigned a value of `"true"` in the `imports` object. - **Object.keys() + forEach**: ```javascript Object.keys(temp).forEach(key => { imports[key] = "true"; }); ``` This approach first retrieves an array of the object's own enumerable property names (keys) using `Object.keys()`, and then iterates over that array with `forEach`. It performs the same assignment as the `for...in` loop. ### Performance Results The benchmark results indicate the number of executions per second for each test case as follows: - **For...in Test**: 3,529,745.5 executions per second. - **Keys + forEach Test**: 3,176,668.25 executions per second. ### Pros and Cons - **For...in Loop**: - **Pros**: - Simple and straightforward syntax. - Operates directly over the object's enumerable properties without creating an intermediate array. - **Cons**: - Can include properties from the prototype chain if not used with caution (i.e., checking with `hasOwnProperty`). This can potentially lead to unexpected results when working with objects that inherit properties. - **Object.keys() + forEach**: - **Pros**: - Only iterates over the object’s own properties (not inherited), which avoids potential pitfalls associated with prototype properties. - Clearer in intent: it explicitly indicates we are using only the own keys of the object. - **Cons**: - Slight overhead from creating an intermediate array with `Object.keys()`. - Potentially less performant than `for...in`, particularly in scenarios with large sets of properties, depending on the JavaScript engine's optimization capabilities. ### Other Considerations - **Use Cases**: The choice between these methods may depend on the context. For most cases where performance is critical, benchmarking in specific environments (like this case demonstrates) is essential to making informed decisions. - **Alternatives**: - **forEach** alone does not work on arrays derived from arrays, but if you're iterating through an array of objects, `Array.prototype.map()` or a simple `for` loop might be considered for clearer syntax and potentially better performance. - **for...of Loop**: While not directly applicable here since `for...of` works with iterable objects (like arrays), using `Array.entries()` or `Array.keys()` could also be considered when working with arrays of objects. In summary, this benchmark illustrates the performance implications of two common JavaScript approaches for iterating over object properties, providing insights into how choice of iteration method can impact execution speed and code clarity.
Related benchmarks:
lodash vs native Clone
testSpeed3
find vs getBYkey
Lodash vs JSON vs Ramda vs old school
Lodash vs JSON vs Ramda vs the oooh geee
Lodash vs JSON vs Ramda vs the dirty
entries
test forin vs keys+foreach+for
Deep copies
Comments
Confirm delete:
Do you really want to delete benchmark?