Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs for-of 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 = { '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
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 (2)
Previous results
Fork
Test case name
Result
for-in
Object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Purpose:** The benchmark is designed to compare the performance of two approaches for iterating over an object's keys in JavaScript: 1. `for...in` loop 2. Using `Object.keys()` function **Options Compared:** * `for-in`: This approach uses a traditional `for...in` loop to iterate over the object's properties (keys). The loop iterates over the property names and their values, allowing you to access both under the same iteration. * `Object.keys()`: This approach uses the `Object.keys()` function to retrieve an array of a given object's own enumerable property names. It then iterates over this array using a traditional `for...of` loop. **Pros and Cons:** * `for-in`: + Pros: - Can access both properties (key-value pairs) under the same iteration. - Can be used for objects that need to iterate over their own properties, like in certain DOM manipulation scenarios. + Cons: - Does not provide any guarantees about the order of property iteration. In older browsers, `for-in` may yield an arbitrary order. - Can lead to slower performance due to the overhead of checking if a property is enumerable. * `Object.keys()`: + Pros: - Provides predictable and consistent ordering of property iteration across all modern browsers. - Does not require any additional processing or checks, making it faster than `for-in`. + Cons: - Only yields the property names (keys) without their values. If you need to access both, this approach requires additional iteration. **Library/Function Used:** In neither of these test cases is a specific library used beyond what's built into JavaScript. However, it's worth noting that `Object.keys()` was introduced in ECMAScript 2015 (ES6) as part of the standard language specification. **Special JS Feature/Syntax:** None are specifically mentioned or required for these benchmark tests. **Other Alternatives:** If you need to iterate over an object's properties, other alternatives might include: * Using a `for...of` loop with an iterator (e.g., `Object.entries()` or `Object.keys()`) * Utilizing modern array methods like `forEach()` * Using libraries like Lodash, which provides an `keys()` function for iterating over object keys Keep in mind that these alternatives might not provide the exact same performance characteristics as the original `for-in` and `Object.keys()` approaches.
Related benchmarks:
function+for-in vs Object.keys
for-in vs object.keys Test2
for-in vs Object.keys()
for-in vs for-in hasOwnProperty vs object.keys forEach
for-in vs for object.keys keys
Comments
Confirm delete:
Do you really want to delete benchmark?