Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For in vs Object.keys.forEach vs For of Object.keys
(version: 0)
Comparing performance of:
for in vs for each vs for of vs for of (outside const)
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = new Object() var keys = (new Array(100)).fill(0).map((x, i) => { return i + 1 }) keys.forEach((x) => { obj['prop' + x] = x })
Tests:
for in
for (var key in obj) { if (obj.hasOwnProperty(key)) console.log(obj[key]) }
for each
Object.keys(obj).forEach(key => console.log(obj[key]))
for of
for (const key of Object.keys(obj)) { console.log(obj[key]) }
for of (outside const)
const keys = Object.keys(obj) for (const key of keys) { console.log(obj[key]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for in
for each
for of
for of (outside const)
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 JSON and explain what is being tested. **Benchmark Definition** The benchmark definition represents different approaches to iterate over an object's properties using `for` loops. There are four variations: 1. **For in**: This method uses the `in` operator to iterate over the object's property names. 2. **For each (Object.keys)**: This method uses the `Object.keys()` function to get an array of property names, and then iterates over it using `forEach`. 3. **For of (with const)**: This method uses the `for...of` loop syntax with a `const` declaration to iterate over an array of property names obtained from `Object.keys()`. 4. **For of (without const)**: This method is similar to the previous one, but the variable declaration (`const key`) is outside the `for` loop. **Options Compared** The four options are compared in terms of execution speed. The benchmark measures how many executions per second each option can handle. **Pros and Cons** Here's a brief summary of each approach: 1. **For in**: This method has the lowest overhead, as it only requires checking if the property exists on the object using `hasOwnProperty()`. However, it can be slower than other methods because it uses a more complex iteration mechanism. 2. **For each (Object.keys)**: This method is generally faster than `for in` because it uses an array-based iteration mechanism, which is optimized for performance. However, it requires an additional function call to get the property names. 3. **For of (with const)**: This method is similar to `for...of`, but with a twist. The variable declaration (`const key`) must be outside the loop, which can make it less readable. It's generally faster than `for in` because it uses a more efficient iteration mechanism. 4. **For of (without const)**: This method is identical to the previous one, except for the variable declaration location. Its performance characteristics are similar to `for...of`. **Library and Special JS Features** None of these options use any external libraries. However, they do employ some specific JavaScript features: 1. **Object.keys()**: This function returns an array of property names on an object. 2. **For...of loop syntax (const)**: This is a modern JavaScript feature that allows iterating over arrays or other iterable objects using the `for...of` loop syntax. **Other Alternatives** Some alternative approaches to iterate over an object's properties include: 1. **Using Array.prototype.forEach()**: You can use the `forEach()` method on an array of property names to iterate over them. 2. **Using a custom iterator**: If you need more control over the iteration process, you can create a custom iterator using the `Symbol.iterator` property. Keep in mind that these alternative approaches may have different performance characteristics than the ones being compared in this benchmark.
Related benchmarks:
For in vs Object.keys.forEach vs. Object.keys+for
For in vs For vs Object.keys.forEach
Some benchmark
For in vs Object.keys for loop vs Object.keys.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?