<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
var makeArr = (randomCeil) => (len) =>
Array.from({
length: len
}, (v, i) => Math.floor(Math.random() * randomCeil));
var arrOfMillion = makeArr(100)(1e6);
var tripleIt = (num) => num * 3;
var isEven = (num) => num % 2 === 0;
const result = [];
arrOfMillion.forEach((item) => {
const tripleItem = tripleIt(item);
if (isEven(tripleItem)) {
result.push(tripleItem);
}
});
const result = arrOfMillion.map(tripleIt).filter(isEven);
const transducer = R.compose(R.filter(isEven), R.map(tripleIt));
const reducer = (acc, val) => (acc.push(val), acc); // same as (acc, val) => { acc.push(val); return acc }
const result = R.transduce(transducer, reducer, [], arrOfMillion);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
forEach | |
mapFilter | |
transduce |
Test name | Executions per second |
---|---|
forEach | 4.5 Ops/sec |
mapFilter | 18.9 Ops/sec |
transduce | 35.7 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Overview
The website MeasureThat.net is used to create and run JavaScript microbenchmarks. The provided JSON data represents a benchmark that tests three different approaches for filtering an array of numbers: forEach
, mapFilter
, and transduce
. The benchmark uses the Ramda library, which is a popular functional programming library for JavaScript.
Benchmark Definition
The benchmark definition json contains two main parts:
arrOfMillion
) and defines three functions: tripleIt
, isEven
, and the Ramda functions R.filter
and R.map
. The script preparation code is used to set up the benchmark environment.Individual Test Cases
There are three test cases:
forEach
method of the array prototype to iterate over the array and filter out odd numbers.tripleIt
function triples each number in the array, and the isEven
function checks if a number is even.map
method to apply the tripleIt
function to the entire array, and then applies the filter
method with the isEven
function to filter out odd numbers.transduce
function, which combines two functions: R.filter
(to filter out odd numbers) and R.map
(to triple each number). The transducer
is defined as a composed version of these two functions.Options Compared
The benchmark compares the performance of three different approaches:
forEach
method.map
and filter
methods of the array prototype.transduce
function to combine two filters.Pros and Cons
Here are some pros and cons for each approach:
forEach
due to caching of intermediate results.Ramda Library
The Ramda library is a popular JavaScript library for functional programming. It provides a set of higher-order functions that operate on arrays and other data structures, making it easier to write composable and efficient code.
Special JS Feature or Syntax
None of the test cases use any special JavaScript features or syntax beyond the standard ECMAScript specification.
Other Considerations
When choosing an approach for filtering an array of numbers, consider factors such as:
In this benchmark, the transduce
approach likely has an advantage due to its functional programming paradigm and use of Ramda's optimized filters. However, the actual performance difference may depend on the specific JavaScript engine and environment used.