Commit dbbae80
perf(Subject): subscription, unsubscription, and nexting improved ~4x (#7240)
* fix(subject): Faster subscriptions management in Subject
The current implementation of the Subject was backing itself on an array of observers. When one observer unsubscribed itself, it was performing a linear scan on the array of observers and dropping the item from the array of observers. The algorithm complexity of this operation was O(n) with n being the number of observers connected to the Subject.
It caused code like:
```js
const allSubscriptions = [];
const source = new Subject();
for (let index = 0 ; index !== 1_000_000 ; ++index) {
allSubscriptions.push(source.subscribe()); // rather quick
}
for (const subscription of allSubscriptions) {
subscription.unsubscribe(); // taking very long
}
```
The proposed approach consists into changing our backing collection (an array) into a Map. But we lose somehow the set capability on subject.observers (might possibly be patched in some way).
Following that change the command `cross-env TS_NODE_PROJECT=tsconfig.mocha.json mocha --config spec/support/.mocharc.js "spec/**/Subje*-spec.ts"` passed to 452ms from 10s.
* refactor(Subject): some perf optimizations
+ Loops over indices of the observers array rather than creating a new iterator and using for-of for each emission
+ Removes unnecessary dirty boolean in favor of clearing the snapshot, which frees up memory a little
+ Centralizes observers list teardown and ensures it's called during `unsubscribe()`.
---------
Co-authored-by: Ben Lesh <ben@benlesh.com>1 parent a292214 commit dbbae80
2 files changed
+83
-22
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
683 | 683 | | |
684 | 684 | | |
685 | 685 | | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
686 | 727 | | |
687 | 728 | | |
688 | 729 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| |||
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
18 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
19 | 30 | | |
20 | | - | |
21 | | - | |
22 | 31 | | |
23 | 32 | | |
| 33 | + | |
24 | 34 | | |
25 | 35 | | |
| 36 | + | |
26 | 37 | | |
27 | 38 | | |
28 | 39 | | |
| |||
48 | 59 | | |
49 | 60 | | |
50 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
51 | 67 | | |
52 | 68 | | |
53 | 69 | | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
59 | 74 | | |
60 | 75 | | |
61 | 76 | | |
| |||
66 | 81 | | |
67 | 82 | | |
68 | 83 | | |
69 | | - | |
70 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
71 | 87 | | |
| 88 | + | |
72 | 89 | | |
73 | 90 | | |
74 | 91 | | |
| |||
77 | 94 | | |
78 | 95 | | |
79 | 96 | | |
80 | | - | |
81 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
82 | 100 | | |
| 101 | + | |
83 | 102 | | |
84 | 103 | | |
85 | 104 | | |
86 | 105 | | |
87 | 106 | | |
88 | | - | |
| 107 | + | |
89 | 108 | | |
90 | 109 | | |
91 | 110 | | |
92 | | - | |
| 111 | + | |
93 | 112 | | |
94 | 113 | | |
95 | 114 | | |
| |||
107 | 126 | | |
108 | 127 | | |
109 | 128 | | |
110 | | - | |
| 129 | + | |
111 | 130 | | |
112 | 131 | | |
113 | 132 | | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
119 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
120 | 140 | | |
121 | 141 | | |
122 | 142 | | |
| |||
0 commit comments