Skip to content

Commit

Permalink
perf(single): remove tryCatch/errorObject (~2.5x 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
------------------------------------------------------------------------------------------
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%
  • Loading branch information
luisgabriel authored and kwonoj committed Feb 2, 2016
1 parent cb75633 commit 2515cfb
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/operator/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -55,20 +52,25 @@ class SingleSubscriber<T> extends Subscriber<T> {

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;

Expand Down

0 comments on commit 2515cfb

Please sign in to comment.