Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Array.prototype.forEach vs _.each vs js for
(version: 0)
Benchmark.js
Compare the new ES6 spread operator with the traditional concat() method
Comparing performance of:
foreach vs lodash each vs for
Created:
6 years ago
by:
Guest
Go 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:
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++);
for
var params = [ "hello", true, 7 ]; var index = 0; for(var i in params){ index++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
foreach
lodash each
for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
foreach
184.5M/s
lodash each
25.6M/s
for
6.2M/s
View exact numbers
Test name
Executions per second
✓
foreach
184,501,328 Ops/sec
lodash each
25,645,412 Ops/sec
for
6,228,598 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the benchmark. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test on MeasureThat.net. The test compares the performance of three different approaches: `Array.prototype.forEach`, `_` (Lodash) `.each`, and traditional `for` loop. **Options Compared** 1. **Array.prototype.forEach**: This method is a built-in JavaScript function that iterates over an array, executing the provided callback function for each element. 2. **_ (Lodash) .each**: Lodash is a popular utility library for JavaScript. The `.each` method is part of it, allowing users to iterate over arrays in a similar way to `forEach`. 3. **Traditional for loop**: A classic approach to iterating over an array using a loop with an index variable. **Pros and Cons** 1. **Array.prototype.forEach**: * Pros: Built-in JavaScript function, easy to use, good performance. * Cons: Can be slower than native code when used in loops or with large arrays. 2. **_ (Lodash) .each**: * Pros: Provides a simple and consistent way to iterate over arrays, especially useful for developers familiar with Lodash. * Cons: Requires an additional library import, might introduce overhead due to the extra function call. 3. **Traditional for loop**: * Pros: Generally fast and efficient, easy to understand and implement. * Cons: Can be error-prone if not implemented correctly, especially when dealing with array indices. **Library (Lodash) and its purpose** Lodash is a utility library that provides a set of functions to perform various tasks in JavaScript, such as iterating over arrays. The `.each` method allows users to iterate over an array, executing the provided callback function for each element. In this benchmark, Lodash is used to provide an alternative implementation of iteration. **Special JS feature or syntax** None mentioned explicitly in the provided code snippets. However, it's worth noting that JavaScript supports various features and syntaxes, such as ES6 promises, async/await, and generators, which might be relevant in more complex benchmarks. **Other alternatives** If you're interested in exploring alternative approaches, consider: * `Array.prototype.forEach` variants: Some browsers or environments provide faster versions of `forEach`, like WebKit's `forEachSlow`. * Iterators and generators: JavaScript provides iterators and generators as a way to iterate over arrays without using traditional loops. * Native WebAssembly (WASM) implementations: Some browsers support WASM, which can be used to run optimized, low-level code for specific tasks. Keep in mind that the best approach depends on the 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
Array.prototype.concat vs spread operator vs lodash.concat - variable and constant
Comments
Confirm delete:
Do you really want to delete benchmark?