Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-of-foreach
(version: 0)
Comparing performance of:
foeach vs forof
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function generateTestArray() { const result = []; for (let i = 0; i < 1000000; ++i) { result.push({ a: i, b: i / 2, r: 0, }); } return result; }
Tests:
foeach
const array = generateTestArray(); array.forEach((x) => { x.r = x.a + x.b; });
forof
const array = generateTestArray(); for (const obj of array) { obj.r = obj.a + obj.b; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
foeach
forof
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):
I'd be happy to help you understand what's being tested in the provided JSON benchmark. **What is being tested?** The benchmark tests two different ways of iterating over an array in JavaScript: `forEach` and `for...of`. Both methods are used to update a property (`r`) in each object within the array. **Options compared** The benchmark compares the performance of two approaches: 1. **`forEach` loop**: This method iterates over the array using a traditional `for` loop, but with a callback function that updates the `r` property of each object. 2. **`for...of` loop**: This method uses a more modern syntax to iterate over the array, where you can directly access properties and perform operations on them. **Pros and Cons** * **`forEach` loop**: + Pros: widely supported, easy to read and write, allows for callback functions. + Cons: generally slower than `for...of` loops due to the overhead of function calls. * **`for...of` loop**: + Pros: faster, more concise, and modern syntax makes it easier to understand. + Cons: less widely supported in older browsers or environments. **Library/Utility used** In this benchmark, no external library is explicitly mentioned. However, the `generateTestArray()` function uses a bit of magic to create an array of 1 million objects with specific properties (`a`, `b`, and `r`). This function is likely included as part of the benchmark itself. **Special JavaScript feature/Syntax** The benchmark uses the `for...of` loop, which is a relatively modern syntax introduced in ECMAScript 2015 (ES6). It's supported in most modern browsers and Node.js versions. If you're using an older environment, you might need to use a polyfill or transpile your code to support this syntax. **Other alternatives** If you want to optimize the `forEach` loop, you could consider: * Using a more efficient data structure, like a `Set` or a `Map`, instead of an array. * Minimizing the number of iterations by using a more concise way to update the properties (e.g., destructuring assignment). * Avoiding unnecessary function calls or closures. For the `for...of` loop, you could consider: * Using a similar array creation method, like `Array.from()` or a custom implementation. * Exploring other modern iteration methods, like `reduce()` or `map()`, depending on your specific use case.
Related benchmarks:
for vs foreach ej
forEach vs for ... of
Test about big arrays
Performance of JavaScript .forEach, .map and .reduce vs for and for..of with 1000p
Comments
Confirm delete:
Do you really want to delete benchmark?