Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For in vs Object.*.forEach vs Object.values 2
(version: 0)
Comparing performance of:
For In vs Object values vs Object keys for of
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = new Object() var keys = (new Array(10000)).fill(0).map((x, i) => { return i + 1 }) keys.forEach((x) => { obj['prop' + x] = x })
Tests:
For In
const arr = []; for (const key in obj) { arr.push(obj[key]); }
Object values
const arr = Object.values(obj);
Object keys for of
const arr = []; for (const key of Object.keys(obj)) { arr.push(obj[key]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
For In
Object values
Object keys for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0
Browser/OS:
Firefox 127 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For In
2418.2 Ops/sec
Object values
12333.5 Ops/sec
Object keys for of
1434.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark is designed to compare three approaches for iterating over an object: `for-in`, `Object.values()`, and `Object.keys()` with the `for...of` loop. The goal is to determine which approach provides the best performance in JavaScript. **Options Compared** 1. **For-In**: This traditional method uses the `in` operator to iterate over the object's own enumerable properties. It can also include inherited properties, which may impact performance. 2. **Object.values()**: This method returns an array of the object's own property values without including inherited properties. It is a more modern and efficient approach. 3. **Object.keys()` with `for...of` loop**: This method uses the `Object.keys()` function to get an array of the object's own enumerable property names, and then iterates over this array using a `for...of` loop. **Pros and Cons of Each Approach** 1. **For-In**: * Pros: Wide browser support, easy to understand. * Cons: May include inherited properties, can be slower due to the need to check for property existence. 2. **Object.values()**: * Pros: Efficient, modern approach that doesn't include inherited properties. * Cons: Requires ES6+ compatibility, may not work in older browsers. 3. **Object.keys()` with `for...of` loop**: * Pros: Similar to `Object.values()`, but allows for more control over property iteration. * Cons: May require more browser support due to the use of `Object.keys()`. **Library and Special JS Features** None are mentioned in the provided benchmark. **Other Considerations** When choosing an approach, consider the following factors: * Browser support: If you need to support older browsers, `for-in` might be a better choice. * Performance: `Object.values()` is generally faster due to its optimized implementation. * Code readability and maintainability: `for...of` loop can make code more concise and readable. **Alternatives** Other approaches for iterating over objects include: 1. **Using `Array.prototype.forEach()`**: This method is similar to `for-in`, but uses an array-based iteration instead of a property-based approach. 2. **Using `Object.getOwnPropertyNames()`**: This method returns an array of all properties (including inherited) on the object, and can be used with `forEach()` or other iteration methods. In summary, the benchmark provides a comprehensive comparison of three approaches for iterating over objects in JavaScript, highlighting their pros and cons, and providing guidance on choosing the best approach based on specific requirements.
Related benchmarks:
For in vs Object.keys.forEach
For in vs Object.keys.forEach FixedForYaRetard
For in vs Object.keys.forEach 10000
Some benchmark
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values v3
Comments
Confirm delete:
Do you really want to delete benchmark?