From 2515cfb2d85885fdebe081a86e2e6afdf1e5b989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Gabriel=20Lima?= Date: Sun, 31 Jan 2016 12:03:48 -0300 Subject: [PATCH] perf(single): remove tryCatch/errorObject (~2.5x 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 ------------------------------------------------------------------------------------------ single-predicate - immediate | 26,054 (±0.55%) | 86,358 (±0.20%) | 3.31x | 231.5% single - immediate | 40,594 (±0.22%) | 1,068,278 (±0.22%) | 26.32x | 2,531.6% After: | RxJS 4.0.7 | RxJS 5.0.0-beta.1 | factor | % improved ------------------------------------------------------------------------------------------ single-predicate - immediate | 26,850 (±0.47%) | 222,364 (±0.39%) | 8.28x | 728.2% single - immediate | 40,725 (±0.75%) | 1,105,053 (±1.32%) | 27.13x | 2,613.5% --- src/operator/single.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/operator/single.ts b/src/operator/single.ts index 7ba02dafe1..6e414e8d2e 100644 --- a/src/operator/single.ts +++ b/src/operator/single.ts @@ -2,9 +2,6 @@ import {Observable} from '../Observable'; import {Operator} from '../Operator'; import {Subscriber} from '../Subscriber'; import {Observer} from '../Observer'; - -import {tryCatch} from '../util/tryCatch'; -import {errorObject} from '../util/errorObject'; import {EmptyError} from '../util/EmptyError'; /** @@ -55,20 +52,25 @@ class SingleSubscriber extends Subscriber { protected _next(value: T): void { const predicate = this.predicate; - const currentIndex = this.index++; - + this.index++; if (predicate) { - let result = tryCatch(predicate)(value, currentIndex, this.source); - if (result === errorObject) { - this.destination.error(errorObject.e); - } else if (result) { - this.applySingleValue(value); - } + this.tryNext(value); } else { this.applySingleValue(value); } } + private tryNext(value: T): void { + try { + const result = this.predicate(value, this.index, this.source); + if (result) { + this.applySingleValue(value); + } + } catch (err) { + this.destination.error(err); + } + } + protected _complete(): void { const destination = this.destination;