<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
var value = {a: 30310, b: 100303, c: 3040494}
_.each(value, function(v,k) {})
Object.entries(value).forEach(function(v,k) {})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash.each | |
native |
Test name | Executions per second |
---|---|
lodash.each | 3001210.0 Ops/sec |
native | 3903491.8 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is tested?
The benchmark measures the performance difference between using Lodash's _.each
function and the built-in Object.forEach()
method in JavaScript.
Options compared:
There are two options being compared:
_.each
: A utility function that applies a callback function to each element of an array-like object.Object.forEach()
: A method that executes a provided function once for each element in an array.Pros and Cons:
_.each
:Object.forEach()
:Library and purpose:
_.each
. Its primary purpose is to provide concise and efficient solutions for common tasks in JavaScript development.Special JS feature or syntax:
There are no special JS features or syntax mentioned in the provided benchmark. Both options use standard JavaScript constructs.
Other alternatives:
If you prefer not to use Lodash, there are other ways to iterate over arrays in JavaScript:
for
loop.Object.forEach()
method, but for true arrays (not array-like objects).Keep in mind that these alternatives may not be as efficient or convenient as using Lodash's _.each
or the built-in Object.forEach()
method.