Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array iteration vs _.each vs map vs set iteration
(version: 0)
Comparing performance of:
_.each vs forEach vs map vs set forEach vs set -> array _.each
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
Script Preparation code:
var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] var set = new Set(array)
Tests:
_.each
_.each(array, (value, index) => { console.log(value) })
forEach
array.forEach((value, index) => { console.log(value) })
map
array.map((value, index) => { console.log(value) })
set forEach
set.forEach((value, index) => { console.log(value) })
set -> array _.each
_.each([...set], (value, index) => { console.log(value) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
_.each
forEach
map
set forEach
set -> array _.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):
Let's dive into the explanation. **Benchmark Overview** The provided benchmark measures the performance of different methods to iterate over an array and a Set in JavaScript: 1. `_.each` (from the Lodash library) 2. `forEach` 3. `map` 4. `set.forEach` 5. `set -> array _.each` The goal is to compare the execution speed of these methods. **Options Comparison** Here's a breakdown of each option: 1. **`.each` (Lodash)**: * Pros: Convenient and concise syntax, suitable for simple iteration. * Cons: May incur overhead due to Lodash's dependency on a library. 2. `forEach`: * Pros: Part of the native JavaScript API, no external dependencies. * Cons: Less convenient syntax compared to `.each`, may require more boilerplate code. 3. `map`: * Pros: Returns a new array, allowing for transformation and filtering. * Cons: May incur additional overhead due to creating a new array, not suitable for simple iteration. 4. `set.forEach`: * Pros: Part of the native JavaScript API, no external dependencies. * Cons: Only available on modern browsers that support Sets, may require additional setup for older browsers. 5. `set -> array _.each`: * Pros: Combines the benefits of using a Set and leveraging `.each`. * Cons: May incur additional overhead due to converting the Set to an array. **Libraries and Special Features** In this benchmark, only the Lodash library is used (`_.each`). There are no special JavaScript features or syntax used beyond the standard APIs. **Other Considerations** When writing performance benchmarks like this one: * **Use a representative dataset**: The provided `array` and `set` variables should give a good representation of typical iteration scenarios. * **Test on multiple devices and browsers**: Including multiple platforms (e.g., Chrome Mobile 87, Android 7.0) helps ensure the results are representative and not browser-specific. * **Use a consistent execution environment**: Make sure the benchmarking process is performed in a controlled environment to minimize external factors affecting performance. **Alternatives** If you're interested in exploring alternative methods or libraries for iteration: * For `forEach`, you can also use `for...of` loops, which are more concise and efficient. * For `map`, consider using `Array.prototype.reduce()` instead of creating a new array. * For Sets, look into libraries like `fast-set` (a faster implementation of the native Set API) or `set` polyfills for older browsers. These alternatives can provide different trade-offs in terms of performance, convenience, and feature set.
Related benchmarks:
Array immutable union: lodash union vs flatten and creating a new set
Array immutable union: set from lodash union vs set from lodash flatten
lodash vs for-of vs forEach vs map v2
my test lodash vs native
Comments
Confirm delete:
Do you really want to delete benchmark?