Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs for-of on object
(version: 0)
compare perf of the constructions for (key in obj) for (key in Object.keys(obj))
Comparing performance of:
for-in vs for-of-keys
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, };
Tests:
for-in
for (var i = 0; i < 10000; i++) { for (var key in obj) { console.log(key); } }
for-of-keys
for (var i = 0; i < 10000; 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
for-of-keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
4.5 Ops/sec
for-of-keys
3.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided benchmark measures the performance of two different JavaScript looping constructs: `for-in` and `for-of`. The test cases compare the execution speed of these loops when iterating over an object's properties. **Options Compared** Two options are compared: 1. **For-in Loop**: This loop uses the `in` operator to iterate over the object's own enumerable properties. 2. **For-of Loop (with Object.keys)**: This loop uses the `for-of` loop with the `Object.keys()` method to iterate over an array of property names. **Pros and Cons** * **For-in Loop**: + Pros: It can be used when you need to access both the property name and value in a single iteration. + Cons: It has performance issues due to the slow property lookup, which can lead to slower execution times. * **For-of Loop (with Object.keys)**: + Pros: It is generally faster than the `for-in` loop since it uses an array of property names instead of performing individual lookups for each iteration. + Cons: It may not work as expected if you need to access both the property name and value in a single iteration, or if you need to iterate over non-enumerable properties. **Library and Purpose** In this benchmark, `Object.keys()` is used as part of the **For-of Loop (with Object.keys)** test case. The `Object.keys()` method returns an array of strings containing all the own enumerable property names of the object. **Special JS Feature/Syntax** None mentioned in the provided benchmark definition. **Other Alternatives** There are other looping constructs available in JavaScript, such as: * **Traditional For Loop**: A classic loop that uses a variable to keep track of the iteration. * **For...of Loop without Object.keys**: This loop can be used when you need to iterate over an array or any other iterable object. Here's an example of how these alternatives might look: ```javascript // Traditional For Loop for (var i = 0; i < 10000; i++) { for (var key in obj) { console.log(key); } } // For...of Loop without Object.keys const keys = []; for (let key of Object.values(obj)) { keys.push(key); } for (let key of keys) { console.log(key); } ``` Note that the performance differences between these alternatives may vary depending on the specific use case and JavaScript engine.
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?