<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
var data = _.range(10000).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 = _.chain(data)
.pick('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);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Ramda without relying on currying or composition | |
Ramda with currying and composition |
Test name | Executions per second |
---|---|
Lodash | 757823.2 Ops/sec |
Ramda without relying on currying or composition | 1007.6 Ops/sec |
Ramda with currying and composition | 938.7 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared, and the pros/cons of each approach.
Benchmark Overview
The benchmark compares the performance of Lodash, Ramda (in two different styles), and "native" JavaScript on the same task. The task involves:
counter
property.isOdd
function.square
function.Library and Purpose
chain
, filter
, map
, and pick
functions are used in the benchmark to perform the specified task.filter
, map
, and pluck
functions directly to perform the task.pipe
function to compose multiple functions together to achieve the desired result.Native JavaScript
The benchmark also includes a native JavaScript implementation of the task, which is used as a baseline for comparison. This approach uses simple array methods like filter
, map
, and forEach
.
Comparison and Pros/Cons
Other Considerations
Alternatives
Other alternatives that could be tested in a benchmark include:
map
and reduce
instead of filter
and forEach