Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash .foreach vs native foreach vs native forof
(version: 0)
Comparing performance of:
lodash vs native vs native forof
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Script Preparation code:
var value = [{a: 30310}, {b: 100303}, {c: 3040494}, {d: 6542321}, {e: 13123531}]
Tests:
lodash
_.forEach(value, function(v,i) {console.log(v)})
native
value.forEach(function(v,i) {console.log(v)})
native forof
for(const v of value){ console.log(v) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash
native
native forof
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:141.0) Gecko/20100101 Firefox/141.0
Browser/OS:
Firefox 141 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
94232.5 Ops/sec
native
95804.5 Ops/sec
native forof
94829.5 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. **Benchmark Overview** The benchmark compares three approaches for iterating over an array: using the `_forEach` function from the Lodash library, the `foreach` method (which is not a standard JavaScript method, but rather a shorthand for `Array.prototype.forEach`), and the `for...of` loop with `const`. **Options Compared** 1. **Lodash `_forEach`**: This option uses the `_forEach` function from the Lodash library, which provides a higher-order function that takes an array as its first argument and a callback function as its second argument. 2. **Native `foreach` method**: This option uses the `foreach` method on an array, which is not a standard JavaScript method. However, it's equivalent to calling `Array.prototype.forEach`. 3. **Native `for...of` loop with `const`**: This option uses a traditional `for...of` loop with `const`, where each iteration of the loop logs the current value. **Pros and Cons** 1. **Lodash `_forEach`**: * Pros: Provides a concise way to iterate over arrays, especially for large datasets. * Cons: Adds an external dependency (the Lodash library), which may increase load times and dependencies. 2. **Native `foreach` method**: Not recommended, as it's not a standard JavaScript method and may cause confusion. 3. **Native `for...of` loop with `const`**: * Pros: No external dependencies or overhead, traditional loop syntax is familiar to many developers. * Cons: May be less efficient than using `forEach`, especially for large datasets. **Library Used** In this benchmark, the Lodash library is used. Lodash provides a set of utilities that can be used to simplify tasks such as array manipulation, object merging, and more. **Special JS Feature/Syntax** The `for...of` loop with `const` is a relatively new feature in JavaScript (introduced in ECMAScript 2015) that allows iterating over iterable objects like arrays. It's designed to provide a more concise way of working with collections compared to traditional `for` loops. **Other Alternatives** For iterating over arrays, other alternatives include: * Using the `Array.prototype.forEach()` method * Using a `while` loop or a `do...while` loop to iterate over an array * Using a library like Underscore.js (which provides similar functionality to Lodash) In general, for simple use cases, using the native `forEach` method or a traditional `for` loop may be sufficient. However, when working with large datasets or complex logic, libraries like Lodash or Underscore.js can provide valuable assistance in simplifying code and improving performance.
Related benchmarks:
lodash .foreach vs native foreach
lodash.each vs Object.forEach vs Native for
Lodash foreach vs native foreach
lodash .foreach vs native foreach vs native for loop
Comments
Confirm delete:
Do you really want to delete benchmark?