<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();
let result = [];
for (let i=0; i<data.length; i++) {
const { counter } = data[i];
if (isOdd (counter)) {
let squared = square (counter);
if (lessThanThreeDigits (squared)) {
result.push (squared);
}
}
}
--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 | |
js native loop |
Test name | Executions per second |
---|---|
JS native | 169.1 Ops/sec |
Ramda with transducer | 45.9 Ops/sec |
Ramda with transducer (no prop) | 167.2 Ops/sec |
Ramda with currying and composition | 50.8 Ops/sec |
Ramda without relying on currying or composition | 50.7 Ops/sec |
Ramda with currying and composition (no pluck) | 183.6 Ops/sec |
Lodash | 151.4 Ops/sec |
js native loop | 243.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is being tested?
The benchmark compares the performance of four different approaches to filter an array of numbers and push the squared value to an output array:
filter
method from Lodash is used to create a new array with only the odd numbers, which are then squared and pushed to an output array.filter
function is used with curried and composited logic to achieve the same result as Lodash.Key insights
filter
method.Comparison
Here's a rough ranking of the approaches by their performance:
Keep in mind that these results may vary depending on the specific use case, input data, and browser/OS configurations.
Takeaways
filter
method is a solid choice when you need to perform multiple operations on an array.I hope this analysis helps!