<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
function double(n) {
return n*2;
}
function add4(n) {
return n + 4;
}
var data = [Array(20)].map((v, idx) => idx);
R.map(R.compose(double, add4), stuff);
data.map(double).map(add4);
_(data).map(double).map(add4);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Ramda | |
Array (native) | |
Lodash |
Test name | Executions per second |
---|---|
Ramda | 0.0 Ops/sec |
Array (native) | 980284.5 Ops/sec |
Lodash | 587395.6 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Definition
The benchmark definition is a JSON object that describes the test case. In this example, we have three test cases: "Ramda", "Array (native)", and "Lodash". The benchmark measures the speed of three different approaches to mapping over an array:
R.map
function from the Ramda library.map
method of JavaScript arrays.map
method from the Lodash library.Test Cases
Each test case has a benchmark definition and a test name. Let's examine each one:
R.map(R.compose(double, add4), stuff);
R.map
function from Ramda to create a new array by applying two functions (double
and add4
) in sequence using R.compose
.data.map(double).map(add4);
map
method of JavaScript arrays to create a new array by applying two functions (double
and add4
) sequentially._(data).map(double).map(add4);
map
method from Lodash to create a new array by applying two functions (double
and add4
) sequentially.Library Explanation
The benchmark uses three libraries:
map
method.JavaScript Feature/Syntax
This test case does not use any special JavaScript features or syntax beyond what is already present in modern JavaScript (ECMAScript 5 and later).
Options Compared
The benchmark compares three different approaches to mapping over an array:
R.map
from the Ramda library.map
method of JavaScript arrays.map
method from Lodash.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
map
from Lodash.Other Alternatives
Other alternatives for mapping over an array include:
flatMap
, reduceRight
): provide additional ways to manipulate arrays in functional programming style.I hope this explanation helps you understand what's being tested in this benchmark!