<script src='https://unpkg.com/rxjs@6.5.5/bundles/rxjs.umd.min.js'></script>
var rawdata = Array.from(Array(100000).keys()) // 10k numbers 1,2,3,4,5...
let { from, Observable } = rxjs;
let { flatMap, map, tap, toArray } = rxjs.operators;
const data$ = from(rawdata)
.pipe(
map(values => {
let count = 0;
for (let i = 0; i < 50; i++) {
count = count + 1;
}
return count;
})
)
let data;
data$.subscribe(d => data = d);
let { from, Observable } = rxjs;
let { flatMap, map, tap, toArray } = rxjs.operators;
const data$ = from(rawdata)
.pipe(
map(values => {
let count = 0;
for (let i = 0; i < 10; i++) {
count = count + 1;
}
return count;
}),
map(values => {
let count = 0;
for (let i = 0; i < 10; i++) {
count = count + 1;
}
return count;
}),
map(values => {
let count = 0;
for (let i = 0; i < 10; i++) {
count = count + 1;
}
return count;
}),
map(values => {
let count = 0;
for (let i = 0; i < 10; i++) {
count = count + 1;
}
return count;
}),
map(values => {
let count = 0;
for (let i = 0; i < 10; i++) {
count = count + 1;
}
return count;
})
)
let data;
data$.subscribe(d => data = d);
let { from, Observable } = rxjs;
let { flatMap, map, tap, toArray } = rxjs.operators;
function myOperator(values) {
let count = 0;
for (let i = 0; i < 10; i++) {
count = count + 1;
}
return count;
}
const data$ = from(rawdata)
.pipe(
map(myOperator),
map(myOperator),
map(myOperator),
map(myOperator),
map(myOperator)
)
let data;
data$.subscribe(d => data = d);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RxJS one pipe | |
Several pipes | |
Many pipes same function |
Test name | Executions per second |
---|---|
RxJS one pipe | 22.3 Ops/sec |
Several pipes | 11.0 Ops/sec |
Many pipes same function | 10.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Overview
The benchmark tests three different approaches to perform a simple operation: counting the number of iterations in a loop. The operations are:
map
and tap
) without functional array functions.myOperator
) for each iteration.Approach 1: One Pipe
This approach uses a single pipe to perform the operation. The map
operator is used to transform the data, and the tap
operator is used to log some information (not actually used in this case). The loop iterates 50 times to increment a counter.
Pros:
Cons:
Approach 2: Multiple Pipes
This approach uses multiple pipes with the same function (myOperator
) for each iteration. The map
operator is used to apply the function to each element, and the result is passed through subsequent operators.
Pros:
Cons:
Approach 3: No Functional Array Functions
This approach does not use any functional array functions, relying on traditional JavaScript loops and variable assignments.
Pros:
Cons:
Libraries and Features
The benchmark uses RxJS (Reactive Extensions for JavaScript), a popular library for reactive programming. The map
operator is used in all three approaches, which applies a transformation function to each element of an observable sequence.
There are no special JS features or syntax mentioned in the benchmark definition, so we won't go into that topic.
Other Alternatives
For this type of microbenchmark, other alternatives could include:
Array.prototype.forEach
instead of loopsfor...of
loops and Promise.all()
Keep in mind that the best approach will depend on the specific requirements of your project, performance considerations, and team expertise.
I hope this explanation helps!