Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object iteration (forEach, for of)
(version: 0)
Comparing performance of:
forEach vs for of
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var object = {}; for (let i = 0; i < 1000; i += 1) { object['something' + i] = 'value'; }
Tests:
forEach
Object.entries(object).forEach(([key, value]) => { console.log(key, value); });
for of
for (const [key, value] of Object.entries(object)) { console.log(key, value); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach
for of
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):
**Benchmark Overview** The provided JSON represents a JavaScript benchmarking test case for measuring the performance of two different ways to iterate over an object: using the `forEach` method and the `for...of` loop with `Object.entries`. The benchmark is designed to be run on various browsers, devices, and platforms. **Tested Options** The benchmark compares the following options: 1. **forEach**: Iterates over the object using the `forEach` method. 2. **For-of**: Iterates over the object using a traditional `for...of` loop with `Object.entries`. **Pros and Cons of Each Approach** * **ForEach**: + Pros: Simple to implement, widely supported by modern browsers. + Cons: May have performance overhead due to function call and closure creation. * **For-of**: + Pros: Can be faster than `forEach` due to reduced function call overhead, more efficient memory usage. + Cons: Requires knowledge of the `for...of` loop syntax and may not be as widely supported by older browsers. **Library Usage** The benchmark uses the `Object.entries()` method, which is a part of the ECMAScript 2015 (ES6) standard. This library provides a convenient way to iterate over objects and their key-value pairs. The `for...of` loop also relies on this library. **Special JS Features or Syntax** * **Arrow functions**: Used in both benchmark definitions (`forEach` and `for-of`). * **Template literals**: Used in the string interpolation of the benchmark script preparation code (`"var object = {};\r\n \r\nfor (let i = 0; i < 1000; i += 1) {\r\n object['something' + i] = 'value';\r\n}"`). **Other Alternatives** If you want to write a similar benchmark, you can use other methods to iterate over an object, such as: * Using `for...in` and accessing the property names directly. * Using a traditional `for` loop with indexing (e.g., `var i = 0; while (i < 1000) { ... }`). * Using a library like Lodash's `forEach` or `each` methods. Keep in mind that each approach has its pros and cons, and the choice of method depends on your specific use case and performance requirements.
Related benchmarks:
Reading object values in loop: Object.keys vs Object.values
For in vs Object.*.forEach vs Object.values vs Object.entries in for loop
For in vs Object.*.forEach vs Object.values vs Object.values in for loop
For in vs Object.*.forEach vs Object.values #3
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values v3
Comments
Confirm delete:
Do you really want to delete benchmark?