Skip to content

Commit

Permalink
perf(every): remove tryCatch/errorObject (~1.7x 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
------------------------------------------------------------------------------------------------
 every-predicate-array - immediate |  95,333 (±0.49%) |  288,517 (±0.30%) |  3.03x |     202.6%
every-predicate-scalar - immediate | 115,688 (±0.52%) |  815,531 (±0.24%) |  7.05x |     604.9%
  every-predicate-this - immediate |  41,195 (±0.25%) |  167,658 (±2.33%) |  4.07x |     307.0%
       every-predicate - immediate |  42,966 (±0.56%) |  159,729 (±0.28%) |  3.72x |     271.8%

After:
                                   |       RxJS 4.0.7 | RxJS 5.0.0-beta.1 | factor | % improved
------------------------------------------------------------------------------------------------
 every-predicate-array - immediate |  95,115 (±0.46%) |  409,389 (±1.33%) |  4.30x |     330.4%
every-predicate-scalar - immediate | 113,744 (±0.50%) |  746,760 (±0.46%) |  6.57x |     556.5%
  every-predicate-this - immediate |  39,560 (±0.50%) |  278,940 (±0.33%) |  7.05x |     605.1%
       every-predicate - immediate |  42,001 (±0.40%) |  269,068 (±0.31%) |  6.41x |     540.6%
  • Loading branch information
luisgabriel committed Feb 2, 2016
1 parent 308cdf7 commit b745978
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/operator/every.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {Operator} from '../Operator';
import {Observer} from '../Observer';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';

/**
* Returns an Observable that emits whether or not every item of the source satisfies the condition specified.
Expand Down Expand Up @@ -44,15 +42,22 @@ class EverySubscriber<T, R> extends Subscriber<T> {
}

protected _next(value: T): void {
const result = tryCatch(this.predicate).call(this.thisArg || this, value, this.index++, this.source);

if (result === errorObject) {
this.destination.error(result.e);
} else if (!result) {
const result = this.tryNext(value);
if (!result) {
this.notifyComplete(false);
}
}

private tryNext(value: T): boolean {
let result = false;
try {
result = this.predicate.call(this.thisArg || this, value, this.index++, this.source);
} catch (err) {
this.destination.error(err);
}
return result;
}

protected _complete(): void {
this.notifyComplete(true);
}
Expand Down

0 comments on commit b745978

Please sign in to comment.