<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
<script type="text/javascript">
window.lodash = _;
_ = null;
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-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 = lodash.chain(data)
.map(item => item.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 = _.chain(data)
.map(item => item.counter)
.filter(isOdd)
.map(square)
.filter(lessThanThreeDigits)
.value();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Ramda without relying on currying or composition | |
Ramda with currying and composition | |
Underscore |
Test name | Executions per second |
---|---|
Lodash | 271.4 Ops/sec |
Ramda without relying on currying or composition | 279.8 Ops/sec |
Ramda with currying and composition | 237.2 Ops/sec |
Underscore | 287.2 Ops/sec |
Let's dive into the benchmark definition and explain what is being tested.
Benchmark Definition
The benchmark is testing the performance of three popular JavaScript libraries: Lodash, Ramda, and Underscore.js (version 1.9.0). The test case is designed to measure the execution speed of a specific data processing pipeline using each library.
Test Case Breakdown
Each test case is defined by a Benchmark Definition
string that outlines the steps involved in processing the data:
var result = lodash.chain(data) ...
lodash.chain()
method to create a chain of functions, allowing for a more functional programming style.var result = R.filter(lessThanThreeDigits, R.map(square, R.filter(isOdd, data)))
var result = R.pipe(R.pluck('counter'), R.filter(isOdd), R.map(square), R.filter(lessThanThreeDigits))(data)
var result = _.chain(data) ...
_.chain()
method, similar to Lodash.Library Purpose
chain()
, map()
, and filter()
.R.pipe()
or currying with R.curry()
.chain()
.Special JS Features
R.curry()
function allows for partial application of a function, creating a new function with a specific number of arguments.R.pipe()
function allows for combining multiple functions into a single pipeline.Other Alternatives
If you're interested in exploring other JavaScript libraries or alternatives:
Pros and Cons
Each library has its strengths and weaknesses:
Keep in mind that this is just a brief overview of the benchmark definition and libraries involved.