<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
function double(n) {
return n*2;
}
var data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];
R.map(double, data);
data.map(double);
_.map(data, double);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Ramda | |
Array (native) | |
Lodash |
Test name | Executions per second |
---|---|
Ramda | 2514868.2 Ops/sec |
Array (native) | 2015388.4 Ops/sec |
Lodash | 3148462.8 Ops/sec |
Let's dive into the details of this benchmark.
What is being tested?
The provided JSON represents a JavaScript microbenchmark that measures the performance of three different approaches:
map
functionmap
function in JavaScript (Array.prototype.map)_map
functionEach test case is defined in the "Individual test cases" section, which includes:
data
) and a transformation function (double
)Options comparison
Here are the options being compared:
map
: This uses a functional programming style to apply the double
function to each element in the data
array.map
: This uses the native JavaScript method for mapping over arrays, which is also a functional programming style._map
: This uses Lodash's implementation of the map
function, which is another functional programming approach.Pros and cons
Here are some pros and cons of each approach:
map
:map
:_map
:map
, depending on the implementationLibrary usage
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, string manipulation, and more. In this benchmark, Lodash's _map
function is used to apply the double
transformation to the input data.
Special JS feature/syntax
This benchmark does not use any special JavaScript features or syntax. It only uses standard JavaScript methods and libraries like Ramda and Lodash.
Other alternatives
If you're interested in alternative approaches, here are a few options:
forEach
loop: Instead of using the native map
function, you could write a custom implementation using a forEach
loop.map
function)However, it's worth noting that these alternatives may not be as efficient or concise as the approaches used in this benchmark.
Benchmark preparation code
The provided "Script Preparation Code" includes:
double
) that doubles its inputdata
) with 30 elementsThe "Html Preparation Code" includes:
Overall, this benchmark provides a useful comparison of different approaches for mapping over arrays in JavaScript.