Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object iteration : for in vs values
(version: 0)
Comparing performance of:
Object.entries() - forEach vs Object - for in
Created:
2 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, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15, p: 16, q: 17, r: 18, s: 19, t: 20, u: 21, v: 22, w: 23, x: 24, y: 25, z: 26 }; var map = new Map(Object.entries(obj));
Tests:
Object.entries() - forEach
Object.values(obj).reduce(function(total, value) { return total + value; }, 0);
Object - for in
let total = 0; for (const key in obj) { total += obj[key]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.entries() - forEach
Object - 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):
The provided JSON represents two benchmark test cases: Object iteration using `for...in` loop and `values()` method, and `Object.entries()` with `forEach()` method. **Options being compared:** 1. **`for...in` loop**: This is an traditional way of iterating over object properties in JavaScript. 2. **`values()` method**: Introduced in ECMAScript 2019, this method returns a new iterable that contains the values of the given object. **Pros and Cons of each approach:** * `for...in` loop: + Pros: - Wide browser support (even older browsers) - Generally faster for small to medium-sized objects + Cons: - Can be slower for large objects due to the overhead of iterating over all properties, including inherited ones - May not work as expected if object prototype chain is complex * `values()` method: + Pros: - Faster for large objects since it only iterates over own enumerable properties - More concise and readable code + Cons: - Not supported in older browsers (before ECMAScript 2019) **Library:** None of the test cases use any external libraries. **Special JS feature or syntax:** * `for...in` loop uses a traditional JavaScript syntax. * `values()` method is a modern JavaScript feature introduced in ECMAScript 2019. **Benchmark Preparation Code:** The preparation code creates an object `obj` with 26 properties and initializes a new `Map` instance from the object's entries using `Object.entries()`. This allows for direct comparison of the two iteration methods. **Other alternatives:** * Another way to iterate over object properties is using a traditional `for` loop with a counter variable. * For more complex objects or when iterating over all properties, including inherited ones, `for...in` loop may be a better choice. * In modern JavaScript environments (ES6+), the `Object.entries()` method and `forEach()` method can also be used together to achieve similar results. It's worth noting that the test cases are designed to compare the performance of different iteration methods in a controlled environment, which is useful for optimizing JavaScript code.
Related benchmarks:
For in vs For of
Object entries vs forin
for-of vs forEach
for in vs for of --
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values v3
Comments
Confirm delete:
Do you really want to delete benchmark?