Skip to content

Commit

Permalink
perf(takeWhile): remove tryCatch/errorObject (~6x 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
----------------------------------------------------------------------------------
takewhile - immediate | 22,885 (±1.53%) |   17,473 (±0.38%) |  0.76x |     -23.6%
            takewhile |  7,270 (±2.49%) |   14,739 (±0.58%) |  2.03x |     102.7%

After:
                      |      RxJS 4.0.7 | RxJS 5.0.0-beta.1 | factor | % improved
----------------------- ----------------------------------------------------------
takewhile - immediate | 21,872 (±1.02%) |  114,560 (±0.39%) |  5.24x |     423.8%
            takewhile |  7,197 (±1.19%) |   93,207 (±1.85%) | 12.95x |   1,195.0%
  • Loading branch information
luisgabriel committed Feb 3, 2016
1 parent d15e72d commit ef6c3c3
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/operator/takeWhile.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';

export function takeWhile<T>(predicate: (value: T, index: number) => boolean): Observable<T> {
return this.lift(new TakeWhileOperator(predicate));
Expand All @@ -27,11 +25,19 @@ class TakeWhileSubscriber<T> extends Subscriber<T> {

protected _next(value: T): void {
const destination = this.destination;
const result = tryCatch(this.predicate)(value, this.index++);
let result: boolean;
try {
result = this.predicate(value, this.index++);
} catch (err) {
destination.error(err);
return;
}
this.nextOrComplete(value, result);
}

if (result == errorObject) {
destination.error(errorObject.e);
} else if (Boolean(result)) {
private nextOrComplete(value: T, predicateResult: boolean): void {
const destination = this.destination;
if (Boolean(predicateResult)) {
destination.next(value);
} else {
destination.complete();
Expand Down

0 comments on commit ef6c3c3

Please sign in to comment.