From c8cf72a2584d20f10c09674803930ccc0c858bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Gabriel=20Lima?= Date: Thu, 4 Feb 2016 14:09:49 -0300 Subject: [PATCH] perf(switchMapTo): remove tryCatch/errorObject (~2x 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 -------------------------------------------------------------------------------------------------- switchmapto-resultselector - immediate | 1,695 (±1.02%) | 12,229 (±1.15%) | 7.21x | 621.3% switchmapto - immediate | 2,910 (±0.80%) | 32,560 (±0.99%) | 11.19x | 1,018.8% switchmapto-resultselector | 2,621 (±0.87%) | 12,473 (±0.49%) | 4.76x | 375.9% switchmapto | 3,300 (±2.11%) | 15,433 (±0.57%) | 4.68x | 367.7% After: | RxJS 4.0.7 | RxJS 5.0.0-beta.1 | factor | % improved -------------------------------------------------------------------------------------------------- switchmapto-resultselector - immediate | 1,645 (±2.07%) | 28,788 (±5.21%) | 17.50x | 1,650.5% switchmapto - immediate | 3,056 (±0.65%) | 31,656 (±1.17%) | 10.36x | 936.0% switchmapto-resultselector | 2,793 (±0.54%) | 14,322 (±1.15%) | 5.13x | 412.7% switchmapto | 3,381 (±4.13%) | 15,969 (±3.86%) | 4.72x | 372.2% --- src/operator/switchMapTo.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/operator/switchMapTo.ts b/src/operator/switchMapTo.ts index afc6f5029e..ea712ff0f8 100644 --- a/src/operator/switchMapTo.ts +++ b/src/operator/switchMapTo.ts @@ -2,8 +2,6 @@ import {Operator} from '../Operator'; import {Observable} from '../Observable'; import {Subscriber} from '../Subscriber'; import {Subscription} from '../Subscription'; -import {tryCatch} from '../util/tryCatch'; -import {errorObject} from '../util/errorObject'; import {OuterSubscriber} from '../OuterSubscriber'; import {InnerSubscriber} from '../InnerSubscriber'; import {subscribeToResult} from '../util/subscribeToResult'; @@ -69,14 +67,23 @@ class SwitchMapToSubscriber 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.tryResultSelector(outerValue, innerValue, outerIndex, innerIndex); } else { destination.next(innerValue); } } + + private tryResultSelector(outerValue: T, innerValue: R, + outerIndex: number, innerIndex: number): void { + const { resultSelector, destination } = this; + let result: R2; + try { + result = resultSelector(outerValue, innerValue, outerIndex, innerIndex); + } catch (err) { + destination.error(err); + return; + } + + destination.next(result); + } }