<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
var data = Array.from(Array(10000000).keys()).map((a) => {
return {
id: a,
name: a
}
});
_.map(data, (d) => d);
data.map(d => d);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Native |
Test name | Executions per second |
---|---|
Lodash | 55.3 Ops/sec |
Native | 12.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Overview
The provided benchmark compares the performance of Lodash.js (a popular utility library for JavaScript) and native JavaScript (without any additional libraries) when executing two simple operations: mapping an array using _.map
from Lodash and data.map()
in native JavaScript.
Script Preparation Code
The script preparation code defines a large dataset, data
, which consists of 10 million objects with id
and name
properties. This data will be used for both benchmark tests.
Html Preparation Code
The HTML preparation code includes a reference to the Lodash.js library version 4.17.4, which is used by the first test case (Lodash
).
Benchmark Test Cases
There are two individual test cases:
_.map(data, (d) => d);
, which calls the map()
function from Lodash on the data
array.data.map(d => d);
, which implements a similar mapping operation without using any external library.Pros and Cons
Here's a brief overview of the pros and cons of each approach:
map()
. It also offers additional functionality out of the box..map()
function in Lodash.Library Used
In this benchmark, Lodash is used to implement the map()
function. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, string manipulation, and more. Its purpose is to provide concise, expressive, and efficient solutions for common programming tasks, making developers' lives easier.
Special JS Features or Syntax
There are no special JavaScript features or syntax used in this benchmark, as both tests follow standard JavaScript syntax.
Other Alternatives
For alternative approaches to mapping arrays, you can explore other methods like:
Array.prototype.forEach()
and returning a new array with transformed valuesArray.prototype.reduce()
and accumulating the transformed valuesmap
-like methods in libraries like Ramda or Immutable.jsKeep in mind that the choice of approach depends on your specific use case, performance requirements, and personal preferences.