Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs for-of vs forEach5453
(version: 0)
Comparing performance of:
lodash.each vs vanilla for-loop w/ Object.keys
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 obj = Array.from({ length: 10000 }).map((value, i) => i).reduce((val, v) => { val[v] = v; return val; }, {})
Tests:
lodash.each
_.each(obj, function(v,k) {})
vanilla for-loop w/ Object.keys
const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { const v = obj[keys[i]]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash.each
vanilla for-loop w/ Object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Overview** The benchmark measures the performance of two approaches: using Lodash's `each` function and a vanilla JavaScript for-loop with `Object.keys`. The goal is to determine which approach is faster. **Test Case 1: Lodash's `each` function** * **Benchmark Definition**: `_.each(obj, function(v,k) {})` + In this case, Lodash's `each` function is used to iterate over the `obj` array. + The `_.each` function takes two arguments: an object (`obj`) and a callback function (the anonymous function that takes two arguments: `v` and `k`). This callback function is not actually used in this benchmark, but it's part of how Lodash's `each` function works. * **Library**: Lodash is a utility library for JavaScript. It provides a collection of helper functions to make common tasks easier. In this case, the `each` function is used to iterate over an array or object. * **Pros**: + Easy to use: `_.each` requires minimal setup and can be used as a one-liner in many cases. + Well-tested: Lodash has extensive testing and is widely used, making it a reliable choice for common tasks. * **Cons**: + Additional dependency: Using Lodash adds an extra dependency to your project. + Performance overhead: While Lodash's functions are optimized, they still introduce additional overhead compared to native JavaScript. **Test Case 2: Vanilla JavaScript for-loop with `Object.keys`** * **Benchmark Definition**: `const keys = Object.keys(obj);\r\nfor (let i = 0; i < keys.length; i++) { const v = obj[keys[i]]; }` + In this case, a vanilla JavaScript for-loop is used to iterate over the `obj` array. + The `Object.keys` function returns an array of property names in the object (`obj`), which are then used as indices in the loop. * **Pros**: + No additional dependencies: This approach doesn't require any external libraries or dependencies. + Well-understood: The for-loop pattern is widely understood and supported by most JavaScript engines. * **Cons**: + More verbose: This implementation requires more code compared to Lodash's `each` function. **Other Considerations** * **Device Platform**: The benchmark was run on a desktop device with Chrome 79. * **Operating System**: The benchmark was run on Windows. * **JavaScript Engine**: The benchmark uses the JavaScript engine implemented in Chrome (V8). **Alternatives** * Other approaches could include: + Using `forEach` instead of Lodash's `each`: `obj.forEach(function(v,k) {})` + Using an arrow function: `_.map(obj, v => v);` + Using a traditional for-loop with indices: `for (let i = 0; i < obj.length; i++) { const v = obj[i]; }` Keep in mind that the results of these alternative approaches may vary depending on the specific JavaScript engine and device platform used.
Related benchmarks:
lodash vs for-of vs forEach
lodash for-in vs native for-in (lodash version: 4.17.10)
lodash vs for-of vs forEach es6
lodash foreach vs for-of vs forEach
Comments
Confirm delete:
Do you really want to delete benchmark?