<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);
var result = _.pick(
_.filter(
_.map(
_.filter(data, lessThanThreeDigits),
square),
isOdd),
'counter');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Ramda without relying on currying or composition | |
Ramda with currying and composition | |
Lodash without chain |
Test name | Executions per second |
---|---|
Lodash | 544867.8 Ops/sec |
Ramda without relying on currying or composition | 1015.9 Ops/sec |
Ramda with currying and composition | 929.5 Ops/sec |
Lodash without chain | 6334.2 Ops/sec |
Let's dive into the explanation of the provided benchmark.
Benchmark Purpose: The benchmark compares the performance of Lodash, Ramda, and native JavaScript on the same task. The task involves:
Options Compared:
_.filter
, _.map
, etc.) and does not use method chaining.Pros and Cons:
Library Usage:
_.range
, _
, .chain()
, etc. functions that simplify the task.R.filter
, R.map
, R.pipe
functions to simplify the task.Other Considerations:
Alternative Approaches:
Keep in mind that the choice of approach depends on the project requirements, performance constraints, and personal preference.