Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash forEach vs for i loop vs native forEach
(version: 0)
Comparing performance of:
lodash.forEach vs native for vs native forEach
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.17.15/lodash.min.js"></script>
Script Preparation code:
var values = [{a: 30310}, {b: 100303}, {c: 3040494}]
Tests:
lodash.forEach
var count = 0; _.forEach(values, function(v,i) { if (v.a != null) { count++; } })
native for
var count = 0; for (var i = 0; i < values.length; i++) { if (values[i].a != null) { count++; } }
native forEach
var count = 0; values.forEach(function(v) { if (v.a != null) { count++; } })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash.forEach
native for
native forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash.forEach
66557124.0 Ops/sec
native for
138886976.0 Ops/sec
native forEach
74817680.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches: 1. **Lodash `forEach`**: A popular JavaScript utility library that provides a functional programming style implementation of the `forEach` method. 2. **Native `for` loop**: The built-in `for` loop in JavaScript, which uses an index variable to iterate over an array. 3. **Native `forEach` method**: Another built-in JavaScript method for iterating over arrays. **Options Being Compared** The options being compared are the execution time and performance of each approach when used to iterate over a sample dataset (`values`) and increment a counter (`count`) whenever the value has a non-null `a` property. The benchmark measures the number of executions per second, which represents the frequency at which the loop iterates. **Pros and Cons** * **Lodash `forEach`**: + Pros: Provides a concise and expressive way to iterate over arrays, eliminating the need for manual indexing. + Cons: May incur additional overhead due to the use of an external library and the creation of a new scope. * **Native `for` loop**: + Pros: Optimized for performance and can be faster than the Lodash implementation. + Cons: Requires manual indexing, which can lead to errors if not implemented correctly. * **Native `forEach` method**: + Pros: Similar to the Lodash implementation in terms of syntax and expressiveness. + Cons: May have similar overhead as the Lodash implementation due to its internal workings. **Library Used** The benchmark uses the Lodash library, which provides a functional programming style implementation of the `forEach` method. The specific version used is 4.17.15. **Special JS Feature or Syntax** None mentioned in the provided code snippets. **Benchmark Preparation Code and Individual Test Cases** The preparation code creates an array (`values`) with three objects containing non-null `a` properties, and then uses each of the three approaches to iterate over this array, incrementing a counter. The individual test cases are: 1. Lodash `forEach` 2. Native `for` loop 3. Native `forEach` method **Latest Benchmark Result** The latest result shows that the native `forEach` method performs best, followed by the native `for` loop, and then the Lodash implementation. **Alternatives** Other alternatives to compare with these approaches could be: * Using a custom-made iterator function to iterate over the array * Comparing the performance of different JavaScript engines (e.g., V8, SpiderMonkey) * Testing the performance of other libraries or frameworks that provide similar iteration methods (e.g., React, Angular) However, for a basic benchmark like this one, native implementations are often the most suitable options.
Related benchmarks:
lodash.each vs Object.forEach vs Native for
Lodash foreach vs native foreach
lodash forEach vs for i loop modified
lodash .foreach vs native foreach vs native forof
lodash .foreach vs native foreach vs native for loop
Comments
Confirm delete:
Do you really want to delete benchmark?