Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash forEach vs array.forEach vs for loop
(version: 0)
Comparing performance of:
lodash.forEach vs array.forEach vs for loop
Created:
6 years ago
by:
Guest
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 values = []; for(i = 0; i < 10000; i++) { if (Math.random() > 0.5) { values.push({ x: true }); } else { values.push({ x: false }); } }
Tests:
lodash.forEach
var count = 0; _.forEach(values, function(v) { if (v.x) { count++; } });
array.forEach
var count = 0; values.forEach(function(v) { if (v.x) { count++; } });
for loop
var count = 0; for (var i = 0; i < values.length; i++) { if (values[i].x) { count++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash.forEach
array.forEach
for loop
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/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash.forEach
9670.5 Ops/sec
array.forEach
29215.6 Ops/sec
for loop
16466.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript benchmark, specifically comparing the performance of three approaches: using Lodash's `forEach` function, the built-in `forEach` method of arrays, and a traditional `for` loop. **Options Compared:** 1. **Lodash's `forEach`**: This is an external library that provides a functional programming interface for iterating over arrays. 2. **Built-in `forEach` method of arrays**: This is a native JavaScript function that allows you to iterate over the elements of an array without mutating it. 3. **Traditional `for` loop**: This is a basic iteration mechanism that uses a counter variable and conditional statements to traverse through the elements of an array. **Pros and Cons:** 1. **Lodash's `forEach`**: * Pros: Provides a concise and expressive way to iterate over arrays, decouples the iteration logic from the rest of the code. * Cons: Requires an external library, which may not be available in all environments (e.g., older browsers). 2. **Built-in `forEach` method of arrays**: * Pros: Native JavaScript function, widely supported across modern browsers and Node.js versions. * Cons: May require additional setup or configuration to access the iteration logic from other parts of the codebase. 3. **Traditional `for` loop**: * Pros: Widely understood and easily implemented, requires no external dependencies. * Cons: Can be verbose and error-prone, especially for complex iterations. **Library Used:** Lodash is a popular JavaScript utility library that provides a wide range of functional programming utilities, including the `forEach` function used in this benchmark. Lodash's purpose is to simplify common tasks by providing pre-built functions and methods that can be used in various parts of codebases. **Special JS Feature/Syntax:** None are explicitly mentioned in the provided JSON. **Other Alternatives:** In addition to the three options compared, other alternatives for iterating over arrays in JavaScript include: 1. **Array.prototype.map()**: This method creates a new array with the results of applying a provided function to each element in the original array. 2. **Array.prototype.filter()**: This method creates a new array with only the elements that satisfy a provided condition. 3. **For...of loop**: This is a newer iteration mechanism introduced in ECMAScript 2015 (ES6), which allows you to iterate over arrays and other iterable objects without using traditional `for` loops. These alternatives may offer different performance characteristics or trade-offs compared to the three options compared in this benchmark, but are often used for different use cases or requirements.
Related benchmarks:
Array.prototype.forEach vs Lodash forEach
native for loop vs Array.prototype.forEach vs lodash forEach
Foreach lodash vs native
lodash.foreach vs for-of vs array.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?