From a55f459255144f9beb7871c37edf779fb74943e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Gabriel=20Lima?= Date: Mon, 1 Feb 2016 16:39:44 -0300 Subject: [PATCH] perf(exhaustMap): remove tryCatch/errorObject (~10% improvement) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before: | RxJS 4.0.7 | RxJS 5.0.0-beta.1 | factor | % improved ---------------------------------------------------------------------------------- exhaustMap - immediate | 1,449 (±0.95%) | 17,023 (±0.88%) | 11.75x | 1,075.1% exhaustMap | 4,732 (±0.34%) | 80,795 (±0.38%) | 17.07x | 1,607.4% After: | RxJS 4.0.7 | RxJS 5.0.0-beta.1 | factor | % improved ---------------------------------------------------------------------------------- exhaustMap - immediate | 1,452 (±1.14%) | 18,523 (±0.68%) | 12.76x | 1,175.5% exhaustMap | 4,941 (±0.29%) | 87,146 (±0.39%) | 17.64x | 1,663.8% --- src/operator/exhaustMap.ts | 42 +++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/operator/exhaustMap.ts b/src/operator/exhaustMap.ts index 0dcf12c3b8..b6dbc09ebd 100644 --- a/src/operator/exhaustMap.ts +++ b/src/operator/exhaustMap.ts @@ -1,8 +1,6 @@ import {Operator} from '../Operator'; import {Observable} from '../Observable'; import {Subscriber} from '../Subscriber'; -import {tryCatch} from '../util/tryCatch'; -import {errorObject} from '../util/errorObject'; import {OuterSubscriber} from '../OuterSubscriber'; import {InnerSubscriber} from '../InnerSubscriber'; import {subscribeToResult} from '../util/subscribeToResult'; @@ -46,15 +44,19 @@ class SwitchFirstMapSubscriber extends OuterSubscriber { protected _next(value: T): void { if (!this.hasSubscription) { - const index = this.index++; - const destination = this.destination; - let result = tryCatch(this.project)(value, index); - if (result === errorObject) { - destination.error(errorObject.e); - } else { - this.hasSubscription = true; - this.add(subscribeToResult(this, result, value, index)); - } + this.tryNext(value); + } + } + + private tryNext(value: T): void { + const index = this.index++; + const destination = this.destination; + try { + const result = this.project(value, index); + this.hasSubscription = true; + this.add(subscribeToResult(this, result, value, index)); + } catch (err) { + destination.error(err); } } @@ -70,17 +72,23 @@ class SwitchFirstMapSubscriber extends OuterSubscriber { innerSub: InnerSubscriber): void { const { resultSelector, destination } = this; if (resultSelector) { - const result = tryCatch(resultSelector)(outerValue, innerValue, outerIndex, innerIndex); - if (result === errorObject) { - destination.error(errorObject.e); - } else { - destination.next(result); - } + this.trySelectResult(outerValue, innerValue, outerIndex, innerIndex); } else { destination.next(innerValue); } } + private trySelectResult(outerValue: T, innerValue: R, + outerIndex: number, innerIndex: number): void { + const { resultSelector, destination } = this; + try { + const result = resultSelector(outerValue, innerValue, outerIndex, innerIndex); + destination.next(result); + } catch (err) { + destination.error(err); + } + } + notifyError(err: any): void { this.destination.error(err); }