Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For in vs Object.*.forEach vs Object.values o++ - 1
(version: 1)
Comparing performance of:
For In vs Object values vs Object keys forEach vs Object entries forEach vs object values forEach
Created:
3 years ago
by:
Registered User
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 }) var o = 0
Tests:
For In
for (var key in obj) { o++ }
Object values
for(let value of Object.values(obj)){ o++ }
Object keys forEach
Object.keys(obj).forEach(key => o++);
Object entries forEach
Object.entries(obj).forEach(([key, value]) => o++);
object values forEach
Object.values(obj).forEach(value => o++);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
For In
Object values
Object keys forEach
Object entries forEach
object values forEach
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 dive into the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of three different ways to iterate over an object's properties in JavaScript: 1. **For-in loop**: Iterates directly over the object's own enumerable properties (i.e., keys). 2. **Object.values()**: Returns an array containing all the values of the object. 3. **forEach() on Object.keys() or Object.entries()**: Uses the `keys()` or `entries()` method to get an array of property names, and then applies the `forEach()` method to iterate over them. **Pros and Cons** * **For-in loop**: This is a simple and intuitive way to iterate over object properties. However, it can be slower than other methods because it iterates directly over the object's internal properties, which may not be what you want. * **Object.values()**: Returns an array of values, which can be more efficient than iterating directly over keys. However, this method only returns the actual values, not their corresponding keys. * **forEach() on Object.keys() or Object.entries()**: This approach is often faster because it uses an optimized internal loop to iterate over the property names. **Library and Special Features** None of these approaches use a specific library. The main feature being tested here is how JavaScript iterates over object properties, which has been part of the language since its inception. **Other Considerations** * **Browser vs Node.js**: The benchmark results show that Chrome 102 on Mac OS X performs best across all test cases. This might be due to optimizations specific to the browser or hardware. * **Device Platform and Operating System**: The performance differences between tests may also depend on the underlying device platform and operating system. **Alternatives** If you need to iterate over an object's properties in a different way, here are some alternatives: 1. **Object.keys() + Array.prototype.forEach()**: Similar to the `forEach()` method on Object keys or entries, but uses a separate array of property names. 2. **for...in loop with object.getOwnEnumeratedProperties()** (not supported in older browsers): An alternative way to iterate over the object's own enumerable properties using a custom method. Keep in mind that while these alternatives may offer different performance profiles, they all achieve similar results: iterating over an object's properties and accessing their corresponding values or keys.
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?