<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 transducer = R.compose (
R.map(({counter}) => 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 transducer (no prop) | |
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 | 94.9 Ops/sec |
Ramda with transducer | 29.0 Ops/sec |
Ramda with transducer (no prop) | 85.5 Ops/sec |
Ramda with currying and composition | 28.8 Ops/sec |
Ramda without relying on currying or composition | 28.4 Ops/sec |
Ramda with currying and composition (no pluck) | 92.2 Ops/sec |
Lodash | 90.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Overview
The provided benchmark compares the performance of three approaches:
All tests perform the following operations on an array of objects:
Options Compared
The benchmark compares four different approaches:
map
and filter
functions to perform the operations, but without relying on currying or composition.chain
function to pipe together multiple functions and apply them to the data.Pros and Cons
Benchmark Results
The benchmark results show the average number of executions per second for each test:
The results suggest that:
Keep in mind that these results are specific to this benchmark and may not generalize to other use cases.