Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array iteration vs _.each vs map
(version: 3)
Comparing performance of:
_.each vs forEach vs map
Created:
7 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']
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) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.each
forEach
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.each
117757.8 Ops/sec
forEach
124651.7 Ops/sec
map
125655.4 Ops/sec
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 three different approaches to iterate over an array: `_.each` (using Lodash), `array.forEach`, and `array.map`. The goal is to determine which approach yields the fastest execution time on a specific JavaScript engine. **Options Compared** 1. **_.each (Lodash)**: This method uses the `_each` function from Lodash, a popular utility library for JavaScript. It iterates over an array using a callback function. 2. **forEach**: This is a native JavaScript method that allows iterating over an array using a callback function. 3. **map**: Although `map` returns a new array, it can be used as a way to iterate over the original array by assigning its result to a variable. **Pros and Cons** 1. **_.each (Lodash)**: * Pros: Encapsulates iteration logic in a reusable function, reduces boilerplate code. * Cons: Requires an additional library (Lodash), may introduce overhead due to its size and complexity. 2. **forEach**: * Pros: Native JavaScript method, minimal overhead. * Cons: Limited control over iteration flow compared to _.each or map. 3. **map**: * Pros: Can be used for both iteration and transformation, flexible. * Cons: Returns a new array, which may not be desirable if memory efficiency is critical. **Library (Lodash)** Lodash is a utility library that provides various functions for tasks such as string manipulation, object manipulation, array manipulation, and more. The `_each` function is a part of Lodash's core functionality, allowing you to iterate over arrays using a callback function. **JavaScript Features** The benchmark does not explicitly use any special JavaScript features or syntax, except for the `=>` syntax in the callback functions (e.g., `(value, index) => { ... }`). This syntax is a shorthand way of defining arrow functions, which are a feature introduced in ECMAScript 2015 (ES6). **Other Alternatives** If you're interested in exploring alternative iteration methods or approaches to benchmarking, consider the following: * `reduce`: Another native JavaScript method for array iteration. * `for...of` loop: A newer syntax for iterating over arrays using a more concise and readable approach. * Other libraries like Underscore.js or Ramda provide similar functionality to Lodash. In summary, the benchmark measures the performance of three different approaches to iterate over an array: _.each (Lodash), forEach, and map. Understanding the pros and cons of each approach can help you choose the best method for your specific use case.
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
array.map vs _.map
lodash _.map vs native map true version
Array Map Vs Lodash Map (1)
Comments
Confirm delete:
Do you really want to delete benchmark?