Skip to content

Commit

Permalink
perf(exhaustMap): remove tryCatch/errorObject (~10% improvement)
Browse files Browse the repository at this point in the history
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%
  • Loading branch information
luisgabriel committed Feb 1, 2016
1 parent f59225b commit a55f459
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/operator/exhaustMap.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -46,15 +44,19 @@ class SwitchFirstMapSubscriber<T, R, R2> extends OuterSubscriber<T, R> {

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);
}
}

Expand All @@ -70,17 +72,23 @@ class SwitchFirstMapSubscriber<T, R, R2> extends OuterSubscriber<T, R> {
innerSub: InnerSubscriber<T, R>): 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);
}
Expand Down

0 comments on commit a55f459

Please sign in to comment.