Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
js forEach vs for..of for @nodejs_ru
(version: 0)
Comparing performance of:
forEach vs for..of
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr_num = []; for (var i = 0; i < 1000; i++) { arr_num.push(i); } function numFn(i) { i = i * 3 * 8; } var arr_str = []; for (var i = 0; i < 1000; i++) { arr_str.push((Math.random() + 1).toString(36).substring(7)); } function strFn(i) { i = i + '_'; } var arr_obj = []; for (var i = 0; i < 1000; i++) { arr_obj.push({ [(Math.random() + 1).toString(36).substring(7)]: true }); } function objFn(i) { i.aaa = false; }
Tests:
forEach
arr_num.forEach((item) => { numFn(item); }); arr_str.forEach((item) => { strFn(item); }); arr_obj.forEach((item) => { objFn(item); });
for..of
for (const element of arr_num) { numFn(element); } for (const element of arr_str) { strFn(element); } for (const element of arr_obj) { objFn(element); }
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):
Let's break down the benchmark and its various components. **Benchmark Definition JSON** The provided JSON represents a JavaScript microbenchmark that tests two approaches for iterating over arrays: `forEach` and `for..of`. The test cases are designed to measure the performance of these approaches on different types of data: numbers, strings, and objects. Here's a breakdown of the benchmark definition: * **Name**: The name of the benchmark is "js forEach vs for..of for @nodejs_ru". * **Description**: There is no description provided. * **Script Preparation Code**: This code prepares the test environment by creating three arrays: `arr_num`, `arr_str`, and `arr_obj`. Each array has 1000 elements, which are initialized with different types of data. The script also defines two functions for each type of data: `numFn`, `strFn`, and `objFn`. * **Html Preparation Code**: There is no HTML preparation code provided. **Individual Test Cases** The benchmark consists of two test cases: 1. **forEach** * This test case uses the `forEach` method to iterate over the three arrays. * For each array, it applies a function that performs some calculation on the element (e.g., `numFn`, `strFn`, or `objFn`). 2. **for..of** * This test case uses the `for..of` loop syntax to iterate over the three arrays. * For each array, it applies a similar function to the element as in the `forEach` test case. **Libraries and Special JS Features** There are no specific libraries mentioned in the benchmark definition. However, the `for..of` loop syntax is a standard JavaScript feature introduced in ECMAScript 2015 (ES6). **Pros and Cons of Different Approaches** * **`forEach` approach**: + Pros: - Widely supported by most browsers and Node.js versions. - Easy to read and understand for developers familiar with the syntax. + Cons: - May be slower than `for..of` due to additional overhead of function calls. * **`for..of` approach**: + Pros: - Can be faster than `forEach` due to reduced overhead from function calls. + Cons: - Not as widely supported by older browsers and Node.js versions. **Other Considerations** The benchmark measures the performance of these two approaches on different types of data. This helps identify which approach is more efficient for specific use cases. **Alternatives** If you need to compare other iteration methods, consider using: * `Array.prototype.map()`: Similar to `forEach`, but returns a new array instead of iterating over the original array. * `Array.prototype.filter()`: Removes elements from an array based on a condition. * Iterators or generators: Alternative approaches for iterating over arrays that can be more memory-efficient. Keep in mind that the choice of iteration method depends on the specific requirements of your project.
Related benchmarks:
while vs for vs forEach vs for of - 1000000 - calc exponent multiple
while vs for vs forEach vs for of - 100000000 - calc exponent multiple
Iterator vs Index for loop (cached array length fixed)
Iterator vs Index for loop (global sum)
Lodash.js vs Native22222yslysl2222
Comments
Confirm delete:
Do you really want to delete benchmark?