import Benchmark from "benchmarkjs-pretty"; import * as Rx from "rxjs"; import * as custom from "../src/index"; import * as zen from "zen-observable"; let counter = 0; async function benchMap() { return new Benchmark("map") .add("RxJs", () => { let res = 0; return new Promise((resolve, reject) => { Rx.Observable.of(1, 2, 3) .pipe(Rx.operators.map(x => x + 1)) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("zen-observable", () => { let res = 0; return new Promise((resolve, reject) => { zen .of(1, 2, 3) .map(x => x + 1) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("fast-observables", () => { let res = 0; return new Promise((resolve, reject) => { custom.Observable.of(1, 2, 3) .pipe(custom.map(x => x + 1)) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("native", () => { let res = 0; return new Promise((resolve, reject) => { [1, 2, 3].map(x => x + 1).forEach(x => (res += x)); resolve(); }).then(() => (counter += res)); }) .run(); } async function benchFilter() { return new Benchmark("filter") .add("RxJs", () => { let res = 0; return new Promise((resolve, reject) => { Rx.Observable.of(1, 2, 1, 2) .pipe(Rx.operators.filter(x => x === 1)) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("zen-observable", () => { let res = 0; return new Promise((resolve, reject) => { zen .of(1, 2, 1, 2) .filter(x => x === 1) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("fast-observables", () => { let res = 0; return new Promise((resolve, reject) => { custom.Observable.of(1, 2, 1, 2) .pipe(custom.filter(x => x === 1)) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("native", () => { let res = 0; return new Promise((resolve, reject) => { [1, 2, 1, 2].filter(x => x === 1).forEach(x => (res += x)); resolve(); }).then(() => (counter += res)); }) .run(); } async function benchTake() { return new Benchmark("take") .add("RxJs", () => { let res = 0; return new Promise((resolve, reject) => { Rx.Observable.of(1, 2, 1, 2) .pipe(Rx.operators.take(3)) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("zen-observable", () => { let res = 0; return new Promise((resolve, reject) => { let i = 0; zen .of(1, 2, 1, 2) .filter(() => { i++; return i <= 3; }) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("fast-observables", () => { let res = 0; return new Promise((resolve, reject) => { custom.Observable.of(1, 2, 1, 2) .pipe(custom.take(3)) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("native", () => { let res = 0; return new Promise((resolve, reject) => { [1, 2, 1, 2].filter((_, i) => i <= 3).forEach(x => (res += x)); resolve(); }).then(() => (counter += res)); }) .run(); } async function benchScan() { return new Benchmark("scan") .add("RxJs", () => { let res = 0; return new Promise((resolve, reject) => { Rx.Observable.of(1, 2, 1, 2) .pipe(Rx.operators.scan((acc, x) => acc + x)) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("zen-observable", () => { let res = 0; return new Promise((resolve, reject) => { zen .of(1, 2, 1, 2) .reduce((acc, x) => acc + x) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("fast-observables", () => { let res = 0; return new Promise((resolve, reject) => { custom.Observable.of(1, 2, 1, 2) .pipe(custom.scan((acc, x) => acc + x)) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("fast-observables (primitive)", () => { let res = 0; return new Promise((resolve, reject) => { custom.Observable.of(1, 2, 1, 2) .pipe(custom.scan()) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("native", () => { let res: number; return new Promise((resolve, reject) => { res = [1, 2, 1, 2].reduce((acc, x) => acc + x, 0); resolve(); }).then(() => (counter += res)); }) .run(); } async function benchMultiple() { return new Benchmark("multiple") .add("RxJs", () => { let res = 0; return new Promise((resolve, reject) => { Rx.Observable.of(1, 2, 1, 2) .pipe( Rx.operators.map(x => x + 10), Rx.operators.scan((acc, x) => acc + x), ) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("zen-observable", () => { let res = 0; return new Promise((resolve, reject) => { zen .of(1, 2, 1, 2) .map(x => x + 10) .reduce((acc, x) => acc + x) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("fast-observables", () => { let res = 0; return new Promise((resolve, reject) => { custom.Observable.of(1, 2, 1, 2) .pipe(custom.map(x => x + 10), custom.scan((acc, x) => acc + x)) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("native", () => { let res: number; return new Promise((resolve, reject) => { res = [1, 2, 1, 2].map(x => x + 10).reduce((acc, x) => acc + x, 0); res += 10; resolve(); }).then(() => (counter += res)); }) .run(); } async function benchFlatMap() { return new Benchmark("flatMap") .add("RxJs", () => { let res = 0; return new Promise((resolve, reject) => { Rx.Observable.of(1, 2, 1, 2) .pipe( Rx.operators.flatMap(x => Rx.Observable.of(x + 1, x + 1, x + 1)), ) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("zen-observable", () => { let res = 0; return new Promise((resolve, reject) => { zen .of(1, 2, 1, 2) .flatMap(x => zen.of(x + 1, x + 1, x + 1)) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .add("fast-observables", () => { let res = 0; return new Promise((resolve, reject) => { custom.Observable.of(1, 2, 1, 2) .pipe(custom.flatMap(x => custom.Observable.of(x + 1, x + 1, x + 1))) .subscribe(x => (res += x), reject, resolve); }).then(() => (counter += res)); }) .run(); } async function runAll() { await benchMap(); await benchFilter(); await benchTake(); await benchScan(); await benchMultiple(); await benchFlatMap(); } runAll();