<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
var data = [];
for (var i = 0; i < 10000; i++) {
data.push({ counter: i });
}
function isOdd(num) {
return num % 2 === 1;
}
function square(num) {
return num * num;
}
function lessThanThreeDigits(num) {
return num.toString().length < 3;
}
var expected = data.map(o => o.counter).filter(isOdd).map(square).filter(lessThanThreeDigits);
var result = _.chain(data)
.map(d => _.get(d, 'counter'))
.filter(isOdd)
.map(square)
.filter(lessThanThreeDigits)
.value();
var result = R.filter(lessThanThreeDigits,
R.map(square,
R.filter(isOdd,
R.pluck('counter', data))));
var result = R.pipe(
R.pluck('counter'),
R.filter(isOdd),
R.map(square),
R.filter(lessThanThreeDigits)
)(data);
var result = data.map(o => o.counter).filter(isOdd).map(square).filter(lessThanThreeDigits);
var result = new Array();
for (var i = 0; i < data.length; i++) {
var c = data[i].counter;
if (isOdd(c)) {
var r = square(c);
if (lessThanThreeDigits(r)) {
result.push(r);
}
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Ramda without relying on currying or composition | |
Ramda with currying and composition | |
VanillaJS functional style | |
VanillaJS procedural |
Test name | Executions per second |
---|---|
Lodash | 1773.3 Ops/sec |
Ramda without relying on currying or composition | 1002.1 Ops/sec |
Ramda with currying and composition | 980.2 Ops/sec |
VanillaJS functional style | 2693.2 Ops/sec |
VanillaJS procedural | 6727.4 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Description
The benchmark compares the performance of three JavaScript libraries: Ramda, Lodash, and VanillaJS (native JavaScript). The test case is designed to measure the execution time of an array processing pipeline that involves:
counter
) from each object in the array.isOdd
function.square
function.Options Compared
The benchmark compares four different approaches:
map
, filter
, and reduce
methods to process the array in a functional programming style.filter
, map
, and reduce
methods, without leveraging its currying or composition features.Pros and Cons
Here are some general pros and cons of each approach:
Library Usage
In this benchmark, we're using:
filter
, map
, and reduce
methods are used to process the array.chain
method is used.Special JS Features
This benchmark doesn't explicitly use any special JavaScript features or syntax. However, it does leverage modern JavaScript concepts like:
=>
)map
, filter
, etc.)Overall, this benchmark provides a solid comparison of the performance differences between using Ramda, Lodash, and VanillaJS for array processing pipelines.