<script src="lodash.js"></script>
var array = [];
for (let i =0; i< 100000;i++) {
array.push({id: i})
}
array.map(n => n.id)
_.map(array,n => n.id)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array | |
lodash |
Test name | Executions per second |
---|---|
array | 816.8 Ops/sec |
lodash | 981.1 Ops/sec |
Let's break down the benchmark and its results.
Benchmark Overview
The benchmark compares the performance of two approaches: using the native JavaScript map
function on an array, and using the popular utility library Lodash with its _.map()
function.
Options Compared
There are two options being compared:
map
: This is a built-in JavaScript function that takes an array as input and returns a new array with the results of applying a provided function to each element._.map()
, which does essentially the same thing as the native JavaScript map
function.Pros and Cons
map
:Library and Purpose
In this benchmark, Lodash is used to provide the _.map()
function. The purpose of Lodash is to provide a set of reusable, modular functions that can simplify common tasks in JavaScript programming.
Special JS Feature or Syntax
There are no special JavaScript features or syntaxes mentioned in the provided benchmark. However, it's worth noting that some versions of JavaScript (e.g., ES6+) may provide additional functional programming features that could potentially affect performance comparisons like this one.
Other Alternatives
If you wanted to compare other approaches for mapping an array, you might consider:
Benchmark Preparation Code
The provided preparation code creates an array with 100,000 elements and pushes a simple object onto it for each iteration. The map
function is applied to this array in two different ways: once using the native JavaScript map
, and once using Lodash's _.map()
.
The test name "test map lodash vs array" clearly indicates that this benchmark is comparing the performance of these two approaches.
Overall, this benchmark provides a useful comparison between two common approaches for mapping an array in JavaScript.