Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.forEach vs _.each
(version: 1)
Compare the new ES6 spread operator with the traditional concat() method
Comparing performance of:
Array.prototype.forEach vs lodash each
Created:
8 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.5/lodash.core.js"></script>
Tests:
Array.prototype.forEach
var params = [ "hello", true, 7 ]; var index = 0; params.forEach(param => index++);
lodash each
var params = [ "hello", true, 7 ]; var index = 0; _.each(params, param => index++);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.forEach
lodash each
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/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.forEach
103297760.0 Ops/sec
lodash each
9193175.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **What is being tested?** The benchmark compares two approaches to iterate over an array: 1. `Array.prototype.forEach` (built-in JavaScript method) 2. `_`.each (method from the Lodash library) The test uses a simple example with three elements in the array: `"hello"`, `true`, and `7`. **Options compared** Two options are being compared: * **Built-in JavaScript**: `Array.prototype.forEach` * **Lodash library**: `_`.each **Pros and Cons of each approach:** **Built-in JavaScript (Array.prototype.forEach)** Pros: * Fast and lightweight * Native support in all modern browsers * Easy to use and understand for most developers Cons: * May not be as efficient as other methods in certain scenarios * Can lead to callback hell if not used carefully **Lodash library (_).each** Pros: * More flexible and powerful than the built-in `forEach` method * Supports additional features like callback functions, iterators, and more * Well-maintained and widely used library Cons: * Requires an external dependency (the Lodash library) * May have a slightly higher overhead due to the additional layer of abstraction * Can be less intuitive for developers who are not familiar with the library **Library (Lodash) purpose** The Lodash library is a popular JavaScript utility belt that provides a wide range of functions and methods for various tasks, such as: * Iteration and looping (`_.each`, `forEach`) * Array manipulation (`map`, `filter`, `reduce`) * String and number manipulation (`toString`, `parseInt`) * Object and function manipulation (`assign`, `merge`) * And many more... **Special JS feature or syntax** There are no special JavaScript features or syntax mentioned in this benchmark. The code uses standard JavaScript syntax and the Lodash library's method name. **Other alternatives** If you're looking for alternative methods to iterate over an array, some other options include: * `for` loop: a traditional loop that uses an index variable * `Array.prototype.map()`: a method that creates a new array with transformed elements (not suitable for modifying the original array) * `Array.prototype.reduce()`: a method that applies a function to each element in the array and accumulates a result Keep in mind that the choice of iteration method depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Array.prototype.concat vs spread operator vs lodash concat
Array.prototype.forEach vs _.each vs jquery each vs Array.prototype.map
Array.prototype.concat vs spread operator vs lodash.concat - variable and constant
Array concat vs spread operator vs push (many)
array find vs some vs lodash
Comments
Confirm delete:
Do you really want to delete benchmark?