Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for of Object.values vs for in a
(version: 0)
Comparing performance of:
for of Object.values vs for in
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = {} for (let i=0; i< 1000; i++) { a[[...Array(5)].map(i=>(~~(Math.random()*36)).toString(36)).join('')] = Math.random()*36; }
Tests:
for of Object.values
for (let row of Object.values(a)) { row += 1; }
for in
for (const key in a) { let row = a[key]; row += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for of Object.values
for in
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 definition and test cases to understand what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of two loops: `for...of` with `Object.values()` and `for...in` with direct object access (`a[key]`). The goal is to iterate over a large array-like object (created using `Array(5).map()`) and increment each value. **Options being compared** 1. **for...of** with `Object.values()`: This loop uses the `Object.values()` method to get an array of the object's values, and then iterates over this array using a `for...of` loop. 2. **for...in** with direct object access (`a[key]`): This loop directly accesses each property of the object using a `for...in` loop and assigns it to a variable `row`, which is then incremented. **Pros and Cons** 1. **for...of** with `Object.values()`: * Pros: More concise and readable, as it leverages the built-in `Object.values()` method. * Cons: May be slower due to the extra function call overhead of `Object.values()`. 2. **for...in** with direct object access (`a[key]`): * Pros: Potentially faster, as it avoids the extra function call and loop variables. * Cons: Less concise and more error-prone, as it requires manual property iteration. **Library** None of the test cases use any external libraries. The `Object.values()` method is a built-in JavaScript feature. **Special JS features or syntax** The benchmark uses modern JavaScript features: 1. **Template literals**: Used in the initial object creation script (`a[[...Array(5)].map(i=>(~~(Math.random()*36)).toString(36)).join('')] = Math.random()*36;`). 2. **Arrow functions**: Used in the initial object creation script (`i=>(~~(Math.random()*36)).toString(36)`). **Other alternatives** Alternative approaches could include: 1. **forEach()**: Instead of using `for...of` or `for...in`, consider using the `Array.prototype.forEach()` method, which is more concise and readable. 2. **while loop**: Another option would be to use a traditional `while` loop instead of `for` loops. **Benchmark preparation code** The script generation code creates an array-like object with 1000 properties, each containing a random value between 0 and 35 (inclusive). This is done using template literals and the spread operator (`[...Array(5)].map()`) to generate the property names. **Individual test cases** There are two individual test cases: 1. **for of Object.values**: Iterates over an array created from `Object.values(a)`, incrementing each value. 2. **for in**: Iterates directly over the properties of object `a`, assigning each value to a variable `row` and incrementing it. Keep in mind that these test cases are designed to compare performance, so the focus is on the loop iteration itself rather than other aspects of the code.
Related benchmarks:
Object keys vs Array map v2
for of Object.values vs for in b
Map vs Object with Number Keys
Map -> Array
Comments
Confirm delete:
Do you really want to delete benchmark?