Skip to content

Commit 6186d46

Browse files
committed
perf(reduce): remove tryCatch/errorObject, optimize calls, 2-3x perf improvement
1 parent 338135d commit 6186d46

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/operator/reduce.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import {Observable} from '../Observable';
22
import {Operator} from '../Operator';
33
import {Subscriber} from '../Subscriber';
4-
import {tryCatch} from '../util/tryCatch';
5-
import {errorObject} from '../util/errorObject';
64

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

51-
protected _next(x: T) {
49+
next(value: T) {
5250
if (this.hasValue || (this.hasValue = this.hasSeed)) {
53-
const result = tryCatch(this.project).call(this, this.acc, x);
54-
if (result === errorObject) {
55-
this.destination.error(errorObject.e);
56-
} else {
57-
this.acc = result;
58-
}
51+
this._tryReduce(value);
5952
} else {
60-
this.acc = x;
53+
this.acc = value;
6154
this.hasValue = true;
6255
}
6356
}
6457

65-
protected _complete() {
58+
private _tryReduce(value: T) {
59+
let result: any;
60+
try {
61+
result = this.project(<R>this.acc, value);
62+
} catch (err) {
63+
this.destination.error(err);
64+
return;
65+
}
66+
this.acc = result;
67+
}
68+
69+
complete() {
6670
if (this.hasValue || this.hasSeed) {
6771
this.destination.next(this.acc);
6872
}

0 commit comments

Comments
 (0)