<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
var array = [{id: 'a'}, {id: 'c'}, {id: 'b'}]
_.map(array, a => console.log(a.id))
array.map(a => console.log(a.id))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.map | |
native |
Test name | Executions per second |
---|---|
_.map | 49777.4 Ops/sec |
native | 64874.4 Ops/sec |
This benchmark tests the performance of two different methods for iterating over an array and logging each item's id
property:
lodash _.map()
: This utilizes the _.map()
function from the Lodash library. Lodash is a popular JavaScript utility library that offers a wide range of functions to simplify common tasks, including working with arrays. native map()
: This uses the built-in map()
method available in modern JavaScript.Options Compared: The benchmark directly compares the execution speed (executions per second) of both methods.
Pros/Cons:
lodash _.map()
:
native map()
:
map()
method syntax.Other Considerations:
_.map()
might be a more convenient choice due to consistency in coding style and familiarity.Alternatives:
forEach()
: While not directly compared in this benchmark, forEach()
is another common method for iterating over arrays. It executes a provided function for each element but doesn't return a new array. It's generally less performant than map()
if you need the result as a new array.Let me know if you have any other questions about this benchmark or JavaScript performance in general!