Skip to content

Commit

Permalink
perf(reduce): remove tryCatch/errorObject, optimize calls, 2-3x perf …
Browse files Browse the repository at this point in the history
…improvement
  • Loading branch information
benlesh committed Jan 27, 2016
1 parent 338135d commit 6186d46
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/operator/reduce.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {Observable} from '../Observable';
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';

/**
* Returns an Observable that applies a specified accumulator function to the first item emitted by a source Observable,
Expand Down Expand Up @@ -48,21 +46,27 @@ export class ReduceSubscriber<T, R> extends Subscriber<T> {
this.hasSeed = typeof seed !== 'undefined';
}

protected _next(x: T) {
next(value: T) {
if (this.hasValue || (this.hasValue = this.hasSeed)) {
const result = tryCatch(this.project).call(this, this.acc, x);
if (result === errorObject) {
this.destination.error(errorObject.e);
} else {
this.acc = result;
}
this._tryReduce(value);
} else {
this.acc = x;
this.acc = value;
this.hasValue = true;
}
}

protected _complete() {
private _tryReduce(value: T) {
let result: any;
try {
result = this.project(<R>this.acc, value);
} catch (err) {
this.destination.error(err);
return;
}
this.acc = result;
}

complete() {
if (this.hasValue || this.hasSeed) {
this.destination.next(this.acc);
}
Expand Down

0 comments on commit 6186d46

Please sign in to comment.