Skip to content

Commit

Permalink
perf(every): remove tryCatch/errorObject (~1.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
--------------------------------------------------------------------------------------------------
 every-predicate-array - immediate |  95,665 (±1.63%) | 1,707,222 (±0.25%) | 17.85x |   1,684.6%
every-predicate-scalar - immediate | 110,190 (±3.14%) | 1,676,966 (±0.59%) | 15.22x |   1,421.9%
  every-predicate-this - immediate |  40,100 (±0.37%) |   161,021 (±0.24%) |  4.02x |     301.6%
       every-predicate - immediate |  39,217 (±1.82%) |   166,973 (±0.46%) |  4.26x |     325.8%

After:
                                   |       RxJS 4.0.7 |  RxJS 5.0.0-beta.1 | factor | % improved
--------------------------------------------------------------------------------------------------
 every-predicate-array - immediate |  98,913 (±1.28%) | 1,752,222 (±1.30%) | 17.71x |   1,671.5%
every-predicate-scalar - immediate | 112,000 (±0.83%) | 1,652,837 (±0.99%) | 14.76x |   1,375.7%
  every-predicate-this - immediate |  39,521 (±0.69%) |   322,943 (±0.80%) |  8.17x |     717.1%
       every-predicate - immediate |  43,363 (±2.19%) |   293,080 (±0.52%) |  6.76x |     575.9%
  • Loading branch information
luisgabriel committed Jan 29, 2016
1 parent a61424d commit 959a596
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/operator/every.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {ScalarObservable} from '../observable/ScalarObservable';
import {ArrayObservable} from '../observable/ArrayObservable';
import {ErrorObservable} from '../observable/ErrorObservable';
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 All @@ -18,22 +16,21 @@ export function every<T>(predicate: (value: T, index: number, source: Observable
thisArg?: any): Observable<boolean> {
const source = this;
if (source._isScalar) {
const result: boolean = tryCatch(predicate).call(thisArg || this, source.value, 0, source);
if (result === errorObject) {
return new ErrorObservable(errorObject.e, source.scheduler);
} else {
try {
const result = predicate.call(thisArg || this, source.value, 0, source);
return new ScalarObservable(result, source.scheduler);
} catch (err) {
return new ErrorObservable(err, source.scheduler);
}
}

if (source instanceof ArrayObservable) {
const array = (<ArrayObservable<T>>source).array;
const result = tryCatch((array: T[], predicate: (value: T, index: number, source: Observable<T>) => boolean, thisArg: any) =>
array.every(<any>predicate, thisArg))(array, predicate, thisArg);
if (result === errorObject) {
return new ErrorObservable(errorObject.e, source.scheduler);
} else {
const array: T[] = (<ArrayObservable<T>> source).array;
try {
const result = array.every(<any>predicate, thisArg);
return new ScalarObservable(result, source.scheduler);
} catch (err) {
return new ErrorObservable(err, source.scheduler);
}
}
return source.lift(new EveryOperator(predicate, thisArg, source));
Expand Down Expand Up @@ -66,12 +63,13 @@ 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) {
this.notifyComplete(false);
try {
const result = this.predicate.call(this.thisArg || this, value, this.index++, this.source);
if (!result) {
this.notifyComplete(false);
}
} catch (err) {
this.destination.error(err);
}
}

Expand Down

0 comments on commit 959a596

Please sign in to comment.