<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
var value = {a: 30310, b: 100303, c: 3040494};
_.map(value, (v) => v)
Object.entries(value).map((v) => v)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash.map | |
native |
Test name | Executions per second |
---|---|
lodash.map | 7465056.5 Ops/sec |
native | 9061339.0 Ops/sec |
This benchmark compares the performance of two methods for mapping over an object: using Lodash's _.map()
function and using JavaScript's built-in Object.entries()
and .map()
.
Options Compared:
lodash.map
: This utilizes the Lodash library's optimized _.map()
function to iterate over the provided object (value
) and apply a mapping function ((v) => v
) to each value, essentially returning a new array with the same values.native
: This approach uses native JavaScript functionalities. It first obtains an array of key-value pairs from the object using Object.entries(value)
, then iterates over this array using .map()
and applies the mapping function (v) => v
.Pros and Cons:
lodash.map
:
native
:
Other Considerations:
The choice between these approaches often depends on factors like:
_.map
might offer a slight advantage.Alternatives:
There are other ways to iterate over objects in JavaScript:
for...in
loop: While older, it's still a valid option for simple iterations.Object.keys()
and .forEach()
: This approach uses Object.keys()
to get an array of keys and then iterates over them using .forEach()
.Remember that the best approach often depends on the specific context and use case.