Skip to content

Commit c4ce2fb

Browse files
committed
perf(mergeMap): extra 1x factor gains from custom tryCatch member function
1 parent 9c1e725 commit c4ce2fb

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/operator/mergeMap.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import {Observable} from '../Observable';
22
import {Operator} from '../Operator';
33
import {Subscriber} from '../Subscriber';
44
import {Subscription} from '../Subscription';
5-
import {tryCatch} from '../util/tryCatch';
6-
import {errorObject} from '../util/errorObject';
75
import {subscribeToResult} from '../util/subscribeToResult';
86
import {OuterSubscriber} from '../OuterSubscriber';
97

@@ -39,7 +37,7 @@ export class MergeMapOperator<T, R, R2> implements Operator<T, R> {
3937

4038
export class MergeMapSubscriber<T, R, R2> extends OuterSubscriber<T, R> {
4139
private hasCompleted: boolean = false;
42-
private buffer: T[] = [];
40+
private buffer: Observable<any>[] = [];
4341
private active: number = 0;
4442
protected index: number = 0;
4543

@@ -50,23 +48,28 @@ export class MergeMapSubscriber<T, R, R2> extends OuterSubscriber<T, R> {
5048
super(destination);
5149
}
5250

53-
protected _next(value: T): void {
51+
protected _next(value: any): void {
5452
if (this.active < this.concurrent) {
55-
const index = this.index++;
56-
const ish = tryCatch(this.project)(value, index);
57-
const destination = this.destination;
58-
if (ish === errorObject) {
59-
destination.error(errorObject.e);
60-
} else {
61-
this.active++;
62-
this._innerSub(ish, value, index);
63-
}
53+
this._tryNext(value);
6454
} else {
6555
this.buffer.push(value);
6656
}
6757
}
6858

69-
private _innerSub(ish: Observable<R>, value: T, index: number): void {
59+
protected _tryNext(value: any) {
60+
let result: any;
61+
const index = this.index++;
62+
try {
63+
result = this.project(value, index);
64+
} catch (err) {
65+
this.destination.error(err);
66+
return;
67+
}
68+
this.active++;
69+
this._innerSub(result, value, index);
70+
}
71+
72+
private _innerSub(ish: any, value: T, index: number): void {
7073
this.add(subscribeToResult<T, R>(this, ish, value, index));
7174
}
7275

@@ -78,17 +81,22 @@ export class MergeMapSubscriber<T, R, R2> extends OuterSubscriber<T, R> {
7881
}
7982

8083
notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number): void {
81-
const { destination, resultSelector } = this;
82-
if (resultSelector) {
83-
const result = tryCatch(resultSelector)(outerValue, innerValue, outerIndex, innerIndex);
84-
if (result === errorObject) {
85-
destination.error(errorObject.e);
86-
} else {
87-
destination.next(result);
88-
}
84+
if (this.resultSelector) {
85+
this._notifyResultSelector(outerValue, innerValue, outerIndex, innerIndex);
8986
} else {
90-
destination.next(innerValue);
87+
this.destination.next(innerValue);
88+
}
89+
}
90+
91+
_notifyResultSelector(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) {
92+
let result: any;
93+
try {
94+
result = this.resultSelector(outerValue, innerValue, outerIndex, innerIndex);
95+
} catch (err) {
96+
this.destination.error(err);
97+
return;
9198
}
99+
this.destination.next(result);
92100
}
93101

94102
notifyComplete(innerSub: Subscription): void {

0 commit comments

Comments
 (0)