Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set iteration-fixed
(version: 2)
Comparing performance of:
for of vs forEach vs array iterate vs _.each
Created:
7 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var x = new Set(); for (var i = 0; i < 100000; i++) { x.add(i); }
Tests:
for of
let sum = 0; for (const item of x) { sum += item; }
forEach
let sum = 0; x.forEach(item => sum += item);
array iterate
let sum = 0; var arr = Array.from(x); for (let i = 0; i < arr.length; i++) { sum += arr[i]; };
_.each
let sum = 0; _.each(Array.from(x), val => sum += val);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for of
forEach
array iterate
_.each
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'll explain the benchmark and provide an overview of the options compared. **Benchmark Overview** The provided benchmark measures the performance of three different approaches for iterating over a Set object in JavaScript: `for...of`, `forEach`, and converting the Set to an array using `Array.from()` followed by a traditional `for` loop. The benchmark also uses Lodash's `_.each` function as a fourth option. **Options Compared** 1. **`for...of`**: This approach iterates over the elements of the Set object using the `for...of` statement, which is a modern iteration method introduced in ECMAScript 2015. 2. **`forEach`**: This approach uses the `forEach` method provided by the Set object to iterate over its elements. 3. **`Array.from()` + Traditional `for` Loop**: This approach converts the Set object to an array using `Array.from()`, which returns a new array containing all elements of the original Set, and then iterates over the array using a traditional `for` loop. 4. **Lodash's `_`.each`**: This approach uses Lodash's `_.each` function to iterate over the elements of the Set object. **Pros and Cons** * **`for...of`**: This approach is modern, concise, and easy to read. However, it may not be supported in older browsers or versions of JavaScript. * **`forEach`**: This approach is also concise and easy to use, but it may have performance implications compared to the traditional `for` loop approach. * **`Array.from()` + Traditional `for` Loop**: This approach can provide good performance and is widely supported across browsers and versions of JavaScript. However, it requires creating an array, which can be memory-intensive for large datasets. * **Lodash's `_`.each`**: This approach provides a convenient and concise way to iterate over the elements of the Set object. However, it relies on Lodash being included in the project, which may add unnecessary dependencies. **Library Usage** The benchmark uses Lodash version 4.17.5, which includes the `_.each` function. Lodash is a popular utility library for JavaScript that provides a collection of functions for tasks such as array manipulation, object iteration, and more. **Special JS Features or Syntax** The benchmark does not use any special JavaScript features or syntax beyond what is required to demonstrate each option. The only feature used is the modern `for...of` statement, which was introduced in ECMAScript 2015. I hope this explanation helps you understand the benchmark and its options! Let me know if you have further questions.
Related benchmarks:
for loop vs. lodash range foreach
for loop vs. lodash range foreach 2
for loop vs. lodash range foreach latest
for vs lodash foreach
for loop vs. lodash range foreach [2022-11-17]
Comments
Confirm delete:
Do you really want to delete benchmark?