const { fluent, interval } = require('@codibre/fluent-iterable');
const rxjs = require('rxjs');
const rxjsOp = require('rxjs/operators');
const Benchmark = require('benchmark');
const ITEMS = 100000;
const FLAT_FACTOR = 10;
const MULTIPLIER1 = 3;
const MULTIPLIER2 = 2;
const QUOTIENT = 5;
const TAKE = 2000;
const benchmarkSuite = new Benchmark.Suite();
function interval2(init, final) {
const items = [];
for(let i = init; i <= final; i++) {
items.push(i);
}
return items;
}
function *map1(it) {
for (const x of it) {
yield x * MULTIPLIER1;
}
}
function *map2(it) {
for (const x of it) {
yield x * MULTIPLIER2;
}
}
function *filter1(it) {
for (const x of it) {
if (x % QUOTIENT === 0) {
yield x;
}
}
}
function *take1(it) {
let count = 0;
for (const x of it) {
count++;
if (count >= TAKE) {
break;
}
yield interval2(x, x + FLAT_FACTOR);
}
}
function *map3(it) {
for (const x of it) {
yield interval2(x, x + FLAT_FACTOR);
}
}
let log = '';
benchmarkSuite.add('fluent', () => {
fluent(interval(1, ITEMS))
.map((x) => x * MULTIPLIER1)
.map((x) => x * MULTIPLIER2)
.filter((x) => x % QUOTIENT === 0)
.map((x) => interval2(x, x + FLAT_FACTOR))
.take(TAKE)
.forEach((x) => x.join(','));
}).add('array operation chain', () => {
interval2(1, ITEMS).map((x) => x * MULTIPLIER1)
.map((x) => x * MULTIPLIER2)
.filter((x) => x % QUOTIENT === 0)
.map((x) => interval2(x, x + FLAT_FACTOR))
.filter((_x, i) => i < TAKE)
.map((x) => x.join(','));
}).add('native iterable', () => {
const it0 = interval2(1, ITEMS);
const it = map3(take1(filter1(map2(map1(it0)))));
for (const x of it) {
x.join(',');
}
}).add('rxjs', () => {
return rxjs.from(interval2(1, ITEMS))
.pipe(
rxjsOp.map((x) => x * MULTIPLIER1),
rxjsOp.map((x) => x * MULTIPLIER2),
rxjsOp.filter((x) => x % QUOTIENT === 0),
rxjsOp.map((x) => interval2(x, x + FLAT_FACTOR)),
rxjsOp.take(TAKE),
rxjsOp.map((x) => x.join(',')),
).toPromise();
}).on('cycle', function(event) {
console.log(String(event.target));
}).on('complete', function() {
log = this.filter('fastest').map('name');
}).run();
`Fastest is ${log}`