<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
var data = _.range(100000).map(function(i) {
return {
counter: i
}
});
function isOdd(num) {
return num % 2 === 1;
}
function square(num) {
return num * num;
}
function lessThanThreeDigits(num) {
return num.toString().length < 3;
}
var result = data
.map(({counter}) => counter)
.filter(isOdd)
.map(square)
.filter(lessThanThreeDigits)
var transducer = R.compose (
R.map(R.prop('counter')),
R.filter(isOdd),
R.map(square),
R.filter(lessThanThreeDigits),
);
var result = R.transduce (
transducer,
(acc, val) => {
acc.push(val);
return acc;
},
[],
data,
);
var result = R.pipe(
R.pluck('counter'),
R.filter(isOdd),
R.map(square),
R.filter(lessThanThreeDigits)
)(data);
var result = R.filter(lessThanThreeDigits,
R.map(square,
R.filter(isOdd,
R.pluck('counter', data))));
var result = R.pipe(
R.map (({counter}) => counter),
R.filter(isOdd),
R.map(square),
R.filter(lessThanThreeDigits)
)(data);
var result = _.chain(data)
.map('counter')
.filter(isOdd)
.map(square)
.filter(lessThanThreeDigits)
.value();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JS native | |
Ramda with transducer | |
Ramda with currying and composition | |
Ramda without relying on currying or composition | |
Ramda with currying and composition (no pluck) | |
Lodash |
Test name | Executions per second |
---|---|
JS native | 167.3 Ops/sec |
Ramda with transducer | 130.6 Ops/sec |
Ramda with currying and composition | 112.8 Ops/sec |
Ramda without relying on currying or composition | 97.8 Ops/sec |
Ramda with currying and composition (no pluck) | 144.6 Ops/sec |
Lodash | 154.5 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares the performance of four JavaScript libraries: Ramda, Lodash, and two styles of native JavaScript (JS native). The task is to process an array of objects, filtering out odd numbers, squaring them, and returning values with less than three digits.
Library and Syntax Explanation
JS Native (JavaScript)
This option uses only native JavaScript features and syntax, without any external libraries.
Options Compared
The benchmark compares the performance of:
Pros and Cons
Performance Results
The benchmark results show that Ramda's transducer-based syntax (option 1) is the fastest, followed closely by Lodash. The native JavaScript option (option 3) is slower, while the two Ramda options with currying and composition are significantly slower than the first one.
Overall, the benchmark suggests that Ramda's transducer-based syntax can provide a good balance between performance and readability, making it a popular choice among developers. Lodash, on the other hand, excels at providing a wide range of utility functions, but may have higher overhead due to its pipeline syntax.