<script src='https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.6.2/rxjs.umd.min.js'></script>
var source = Array(1000000).fill(1).map((_, i) => Math.random());
const arrayResult = source
.map(n => n * 2)
.filter(n => n > 0.5)
.reduce((m, c) => Math.max(m, c));
const transducerResult = rxjs.of(source).pipe(
rxjs.operators.map(n => n * 2),
rxjs.operators.filter(n => n > 0.5),
rxjs.operators.reduce((m, c) => Math.max(m, c))
).subscribe(console.log);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array | |
RxJs |
Test name | Executions per second |
---|---|
Array | 20.8 Ops/sec |
RxJs | 10.3 Ops/sec |
Let's break down the benchmark and its options.
Benchmark Overview
The benchmark compares the performance of two approaches: Array
and RxJS (a reactive programming library). The test case filters and transforms an array of 1 million random numbers to find the maximum value greater than 0.5.
Options Compared
There are two approaches compared:
map
, filter
, and reduce
methods.Pros and Cons
Library and Purpose
The rxjs
library is used in the benchmark. It provides a set of operators that can be combined to create complex data processing pipelines. In this case, the pipeline includes:
map
: Applies a transformation function to each element of the array.filter
: Filters out elements that don't meet the specified condition.reduce
: Reduces the array to a single value.Special JS Feature/Syntax
There is no special JavaScript feature or syntax used in this benchmark. It's purely functional programming with a focus on performance comparison.
Other Alternatives
If you're interested in exploring alternative approaches, here are some options:
In conclusion, the benchmark provides a clear comparison between two approaches: traditional JavaScript arrays versus RxJS pipelines. The choice of approach depends on your specific use case, performance requirements, and personal preference.