Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for in vs iterate over keys
(version: 0)
Comparing performance of:
for in vs iterate over keys
Created:
4 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 }; var objKeys = ['a','b','c','d','e','f','g'];
Tests:
for in
for (var i=10000; i > 0; i--) { for (var key in obj) { console.log(obj[key]); } }
iterate over keys
for (var i=10000; i > 0; i--) { objKeys.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
iterate over keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for in
4.1 Ops/sec
iterate over keys
3.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** The provided JSON benchmark test case measures the performance difference between two approaches to iterate over the keys of an object: 1. Using `for...in` loop 2. Using `forEach()` method with an array of keys **Options compared:** * **For-in loop**: Iterates over the object's own enumerable properties, including inherited properties. * **ForEach() method**: Iterates over a sequence (array, iterable) and applies a callback function to each element. **Pros and Cons of each approach:** * **For-in loop**: + Pros: - Can access both own properties and inherited properties using the `in` keyword. - Can be used to iterate over objects that don't support `forEach()`. + Cons: - May have performance issues due to slow iteration over inherited properties. - Less predictable behavior when iterating over objects with complex inheritance hierarchies. * **ForEach() method**: + Pros: - More efficient and faster than the for-in loop, especially for large arrays. - More predictable behavior, as it's designed for iterating over sequences. + Cons: - Only works on arrays or iterables (not objects). - Does not provide access to inherited properties. **Special JS feature/syntax:** There are no special JavaScript features or syntaxes used in this benchmark. The test focuses solely on the performance comparison between two iteration approaches. **Library usage:** None of the test cases use any external libraries. The `forEach()` method is a built-in JavaScript function, while the `for...in` loop uses only standard language constructs. **Other alternatives:** If you're looking for alternative ways to iterate over object keys, consider using: * **Object.keys()**: Returns an array of own enumerable property names. * **Object.getOwnPropertyNames()**: Returns all own properties (including inherited) as an array. * **for...of loop**: Introduced in ECMAScript 2015, this loop iterates over iterable objects, including arrays and sets. Keep in mind that the choice of iteration method depends on your specific use case, performance requirements, and language version.
Related benchmarks:
for-in vs Object.keys()
for-in vs object.keys in loop
for-in vs object keys map vs object keys loop
print keys for-in vs object.keys
for-in vs object.keys test TBM
Comments
Confirm delete:
Do you really want to delete benchmark?