Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash forEach vs ramda forEach vs array.forEach vs for loop
(version: 4)
Comparing performance of:
lodash.forEach vs array.forEach vs for loop vs ramda.forEach
Created:
6 years ago
by:
Registered User
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 src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.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
let count = 0; _.forEach(values, v => { if (v.x) { count++; } });
array.forEach
let count = 0; values.forEach(v => { if (v.x) { count++; } });
for loop
const numValues = values.length; let count = 0; for (let i = 0; i < numValues; ++i) { if (values[i].x) { count++; } }
ramda.forEach
let count = 0; R.forEach(v => { if (v.x) { count++; } }, values);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash.forEach
array.forEach
for loop
ramda.forEach
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash.forEach
9238.3 Ops/sec
array.forEach
39291.7 Ops/sec
for loop
19473.7 Ops/sec
ramda.forEach
19859.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested on MeasureThat.net. **Benchmark Overview** The benchmark measures the performance of four different approaches to iterate over an array: 1. **Lodash `forEach`**: uses the `_` (underscore) object from Lodash, which is a utility library for JavaScript. 2. **Array `forEach`**: uses the native `forEach` method on arrays. 3. **For Loop**: uses a traditional `for` loop to iterate over the array. 4. **Ramda `forEach`**: uses the `R.forEach` function from Ramda, which is another utility library for functional programming in JavaScript. **Comparison of Approaches** Here's a brief overview of each approach: * **Lodash `forEach`**: This approach uses the `_` object to provide additional functionality on top of the native array methods. It can be convenient for certain tasks, but may add overhead due to the presence of this extra object. * **Array `forEach`**: This is the most lightweight approach, using only built-in JavaScript features. It's a good choice when performance is critical and you want to avoid adding unnecessary dependencies. * **For Loop**: While not the most elegant solution, traditional `for` loops can be effective for certain use cases. They provide direct control over the iteration process but require more manual effort to manage indices and loop termination. * **Ramda `forEach`**: This approach uses a functional programming style, which can be beneficial when you want to abstract away low-level details and focus on higher-level logic. However, it may introduce additional overhead due to the complexity of the Ramda library. **Library Usage** In this benchmark: * Lodash is used for its `_` object, which provides additional functionality on top of native array methods. * Ramda is used for its `R.forEach` function, which is a higher-order function that takes an array as input and applies a given function to each element. **Special JS Features or Syntax** None of the approaches in this benchmark rely on special JavaScript features or syntax. They all use standard ECMAScript features. **Other Alternatives** If you're looking for alternative ways to iterate over arrays, you might consider: * `forEach` with arrow functions (e.g., `values.forEach((v) => { /* code */ })`) * `map`, `filter`, and other array methods that provide more functional programming-inspired abstractions * Other libraries like jQuery or vanilla JavaScript alternatives like `reduce()` or `forEach` with custom callbacks Keep in mind that each approach has its trade-offs, and the choice ultimately depends on your specific use case and performance requirements.
Related benchmarks:
native for loop vs Array.prototype.forEach vs lodash forEach
lodash vs nativejs foreach
big lodash vs nativejs foreach
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
Array.prototype.every vs Lodash every()
Comments
Confirm delete:
Do you really want to delete benchmark?