<script src="https://rawgit.com/ramda/ramda/master/dist/ramda.js"></script>
var ids = Array.from({ length: 10000 }, (_, index) => index + 1)
var isPositive = i => i % 2 === 0
ids.map(R.identity).filter(isPositive).map(R.inc)
R.pipe(
R.map(R.identity),
R.filter(isPositive),
R.map(R.inc)
)(ids)
const transform = R.pipe(
R.map(R.identity),
R.filter(isPositive),
R.map(R.inc)
)
R.transduce(
transform,
R.flip(R.append),
[],
ids,
);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array(map + filter) | |
ramda(map + filter) | |
ramda-transducer(map + filter) |
Test name | Executions per second |
---|---|
Array(map + filter) | 1987.0 Ops/sec |
ramda(map + filter) | 3842.9 Ops/sec |
ramda-transducer(map + filter) | 18.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and analyze what's being tested on MeasureThat.net.
Benchmark Definition
The benchmark is based on the Ramda library, which provides functional programming utilities for JavaScript. The script preparation code includes a simple array of 10,000 numbers from 1 to 10000, along with two functions: isPositive
that checks if a number is even, and R.identity
, R.map
, R.filter
, and R.inc
which are Ramda's functional programming helpers.
Test Cases
There are three test cases:
map
and filter
methods directly on the array, without any additional context or transformation.map
and filter
functions to perform the same operation as the previous one, but with the added benefit of functional programming abstractions.transduce
function, which applies a transformation (in this case, the same map
and filter
operations) to an array while accumulating results in a separate data structure.Options Compared
The three test cases compare different approaches to performing the same operation:
map
and filter
) on the original array.R.map
, R.filter
, etc.) to perform the same operation, with added abstraction and potential performance benefits.transduce
function to apply a transformation to an array while accumulating results in a separate data structure. This approach can provide better performance and handling of large datasets.Pros and Cons
Here are some pros and cons for each approach:
Library and Syntax
The R
prefix in Ramda's functions (R.map
, R.filter
, etc.) refers to the Ramda namespace. The Ramda library provides a set of functional programming helpers that can simplify code and improve performance.
There are no special JavaScript features or syntax used in these test cases, just standard JavaScript with native methods and Ramda's functional programming helpers.
Alternatives
Other alternatives for performing similar operations include:
map
, filter
, etc.) without any additional libraries.However, it's worth noting that Ramda's transducer approach can provide better performance and handling of large datasets compared to other alternatives, especially when dealing with complex data transformations.