Skip to content

Commit 280b985

Browse files
committed
perf(forkJoin): improve forkJoin perf slightly by removing unnecessary context tracking
1 parent 38b0b24 commit 280b985

File tree

1 file changed

+30
-49
lines changed

1 file changed

+30
-49
lines changed

src/observable/ForkJoinObservable.ts

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -56,82 +56,63 @@ export class ForkJoinObservable<T> extends Observable<T> {
5656
}
5757
}
5858

59-
interface ForkJoinContext {
60-
completed: number;
61-
total: number;
62-
values: Array<any>;
63-
haveValues: Array<boolean>;
64-
selector: Function;
65-
}
66-
6759
class ForkJoinSubscriber<T> extends OuterSubscriber<T, T> {
68-
private context: ForkJoinContext = null;
60+
private completed = 0;
61+
private total: number;
62+
private values: any[];
63+
private haveValues = 0;
6964

7065
constructor(destination: Subscriber<T>,
7166
private sources: Array<SubscribableOrPromise<any>>,
72-
resultSelector?: (...values: Array<any>) => T) {
67+
private resultSelector?: (...values: Array<any>) => T) {
7368
super(destination);
7469

7570
const len = sources.length;
76-
this.context = { completed: 0,
77-
total: len,
78-
values: new Array(len),
79-
haveValues: new Array(len),
80-
selector: resultSelector };
71+
this.total = len;
72+
this.values = new Array(len);
73+
74+
for (let i = 0; i < len; i++) {
75+
const source = sources[i];
76+
const innerSubscription = subscribeToResult(this, source, null, i);
8177

82-
this.tryForkJoin();
78+
if (innerSubscription) {
79+
(<any> innerSubscription).outerIndex = i;
80+
this.add(innerSubscription);
81+
}
82+
}
8383
}
8484

8585
notifyNext(outerValue: any, innerValue: T,
8686
outerIndex: number, innerIndex: number,
8787
innerSub: InnerSubscriber<T, T>): void {
88-
const context = this.context;
89-
90-
context.values[outerIndex] = innerValue;
91-
context.haveValues[outerIndex] = true;
88+
this.values[outerIndex] = innerValue;
89+
if (!(<any>innerSub)._hasValue) {
90+
(<any>innerSub)._hasValue = true;
91+
this.haveValues++;
92+
}
9293
}
9394

9495
notifyComplete(innerSub: InnerSubscriber<T, T>): void {
95-
const outerIndex = (<any>innerSub).outerIndex;
96-
this.tryComplete(outerIndex);
97-
}
98-
99-
private tryComplete(index: number): void {
10096
const destination = this.destination;
101-
const context = this.context;
97+
const { haveValues, resultSelector, values } = this;
98+
const len = values.length;
10299

103-
context.completed++;
104-
105-
if (!context.haveValues[index]) {
100+
if (!(<any>innerSub)._hasValue) {
106101
destination.complete();
102+
return;
107103
}
108104

109-
const values = context.values;
110-
if (context.completed !== values.length) {
105+
this.completed++;
106+
107+
if (this.completed !== len) {
111108
return;
112109
}
113110

114-
if (context.haveValues.every(x => x === true)) {
115-
const value = context.selector ? context.selector.apply(this, values) :
116-
values;
111+
if (haveValues === len) {
112+
const value = resultSelector ? resultSelector.apply(this, values) : values;
117113
destination.next(value);
118114
}
119115

120116
destination.complete();
121117
}
122-
123-
private tryForkJoin(): void {
124-
const sources = this.sources;
125-
const len = sources.length;
126-
127-
for (let i = 0; i < len; i++) {
128-
const source = sources[i];
129-
const innerSubscription = subscribeToResult(this, source, null, i);
130-
131-
if (innerSubscription) {
132-
(<any> innerSubscription).outerIndex = i;
133-
this.add(innerSubscription);
134-
}
135-
}
136-
}
137118
}

0 commit comments

Comments
 (0)