<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.30.1/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 |
---|---|
Loadsh | |
Ramda without currying | |
Ramda with test case |
Test name | Executions per second |
---|---|
Loadsh | 1096651.1 Ops/sec |
Ramda without currying | 2926.3 Ops/sec |
Ramda with test case | 2598.4 Ops/sec |
Measuring performance differences between two popular JavaScript libraries, Ramda and Lodash, is crucial for developers who need to optimize their code.
Benchmark Test Case Analysis
The provided benchmark test cases use both Lodash and Ramda to perform similar operations on a dataset. The goal is to determine which library performs better in terms of execution speed.
Lodash (latest version)
_.chain(data)
: Creates a new chainable object from the input data..pick('counter')
: Selects only the 'counter' property from each element in the chain..filter(isOdd)
: Filters out elements where isOdd
returns false..map(square)
: Applies the square function to each remaining element..filter(lessThanThreeDigits)
: Filters out elements with more than 3 digits..value()
: Returns the final filtered array.The test case using Lodash's pipeline approach (var result = _.chain(data)...
) tests how efficient it is to use a series of functions chained together.
Ramda
R.filter
function filters out elements based on a predicate function.R.map
function applies a given function to each element in the input data.R.pluck
function returns an array of selected values from an object or array.R.pipe
function is used to compose multiple functions together.Test Cases
There are three test cases:
var result = R.filter(lessThanThreeDigits, ...)
).var result = R.pipe(...)(data)
) to perform a series of operations.Pros and Cons
R.pipe
.Other Considerations
Benchmark Results
The provided benchmark results show the execution speed of each library on a specific test case. The RawUAString values indicate the browser and device platform used to run the tests.