Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs Object.keys()
(version: 0)
Comparing performance of:
for-in vs Object.keys forEach vs Object.keys for loop
Created:
5 years ago
by:
Registered User
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 forEach
for (var i=10000; i > 0; i--) { Object.keys(obj).forEach(key => console.log(key)); }
Object.keys for loop
for (var i=10000; i > 0; i--) { const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { console.log(obj[keys[i]]); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-in
Object.keys forEach
Object.keys for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
4.8 Ops/sec
Object.keys forEach
4.0 Ops/sec
Object.keys for loop
4.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided benchmark compares the performance of three different approaches to iterate over an object's keys: `for-in`, `Object.keys()` with a traditional `forEach` loop, and `Object.keys()` followed by a for loop. **For-In Loop** This approach uses the `in` operator to iterate over the object's properties. The `for-in` loop is not designed to work directly with objects; instead, it returns an iterator that allows you to access each property name. This means that the loop variable `key` is a string containing the property name, followed by `!` and then the property value. **Object.keys() with Traditional forEach Loop** This approach uses the `Object.keys()` method to get an array of the object's property names. It then iterates over this array using a traditional `forEach` loop, logging each key to the console. **Object.keys() followed by For Loop** This approach uses `Object.keys()` to get an array of the object's property names, and then iterates over this array using a for loop, logging each value associated with the property name to the console. Pros and Cons: * **For-In Loop**: Pros - It's straightforward and easy to understand. However, it can be slower due to its overhead. * Cons: It returns an iterator, not an array, which makes it harder to work with. * Another con is that the loop variable `key` contains both the property name and value (`!`), making it difficult to use in subsequent iterations. * **Object.keys() with Traditional forEach Loop**: Pros - This approach can be faster than using a for-in loop since it avoids some of its overhead. However, creating an array of keys may require additional memory. * Cons: The traditional `forEach` loop is not optimized for arrays and might incur some performance overhead. * **Object.keys() followed by For Loop**: Pros - This approach uses the most efficient method to get the property names (an array), then iterates over it using a for loop. It avoids the overhead of a traditional `forEach` loop. * Cons: Creating an array and assigning its length to a variable can be slower than just iterating directly. Additional Considerations: * The use of a traditional `for` loop after calling `Object.keys()` is more efficient than using a traditional `forEach` loop because it avoids the overhead associated with creating and then iterating over an iterator (as returned by `Object.keys()`). Other Alternatives: * Using a for-of loop instead of a traditional `for` loop can also be faster since it's optimized to work directly with arrays and iterators. In terms of performance, `Object.keys()` followed by a for loop tends to perform best because it minimizes overhead. This is the recommended approach in the benchmark.
Related benchmarks:
function+for-in vs Object.keys
Object.keys(obj)[0] vs for in
for-in vs for object.keys keys
for-in vs object keys map vs object keys loop
Comments
Confirm delete:
Do you really want to delete benchmark?