Skip to content

Commit a4fec79

Browse files
committed
Reduce SortableSet memory consumption
1 parent 6922a5e commit a4fec79

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

lib/util/SortableSet.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"use strict";
22

3-
module.exports = class SortableSet extends Set {
3+
class SortableSet extends Set {
44

55
constructor(initialIterable, defaultSort) {
66
super(initialIterable);
77
this._sortFn = defaultSort;
88
this._lastActiveSortFn = null;
9-
this._cache = null;
10-
this._cacheOrderIndependent = null;
9+
this._cache = undefined;
10+
this._cacheOrderIndependent = undefined;
1111
}
1212

1313
/**
@@ -16,21 +16,21 @@ module.exports = class SortableSet extends Set {
1616
*/
1717
add(value) {
1818
this._lastActiveSortFn = null;
19-
this._cache = null;
20-
this._cacheOrderIndependent = null;
19+
this._invalidateCache();
20+
this._invalidateOrderedCache();
2121
super.add(value);
2222
return this;
2323
}
2424

2525
delete(value) {
26-
this._cache = null;
27-
this._cacheOrderIndependent = null;
26+
this._invalidateCache();
27+
this._invalidateOrderedCache();
2828
return super.delete(value);
2929
}
3030

3131
clear() {
32-
this._cache = null;
33-
this._cacheOrderIndependent = null;
32+
this._invalidateCache();
33+
this._invalidateOrderedCache();
3434
return super.clear();
3535
}
3636

@@ -50,7 +50,7 @@ module.exports = class SortableSet extends Set {
5050
super.add(sortedArray[i]);
5151
}
5252
this._lastActiveSortFn = sortFn;
53-
this._cache = null;
53+
this._invalidateCache();
5454
}
5555

5656
/**
@@ -65,7 +65,7 @@ module.exports = class SortableSet extends Set {
6565
* @returns {any} - returns result of fn(this), cached until set changes
6666
*/
6767
getFromCache(fn) {
68-
if(this._cache === null) {
68+
if(this._cache === undefined) {
6969
this._cache = new Map();
7070
} else {
7171
const data = this._cache.get(fn);
@@ -77,12 +77,13 @@ module.exports = class SortableSet extends Set {
7777
this._cache.set(fn, newData);
7878
return newData;
7979
}
80+
8081
/**
8182
* @param {Function} fn - function to calculate value
8283
* @returns {any} - returns result of fn(this), cached until set changes
8384
*/
8485
getFromUnorderedCache(fn) {
85-
if(this._cacheOrderIndependent === null) {
86+
if(this._cacheOrderIndependent === undefined) {
8687
this._cacheOrderIndependent = new Map();
8788
} else {
8889
const data = this._cacheOrderIndependent.get(fn);
@@ -94,4 +95,16 @@ module.exports = class SortableSet extends Set {
9495
this._cacheOrderIndependent.set(fn, newData);
9596
return newData;
9697
}
97-
};
98+
99+
_invalidateCache() {
100+
if(this._cache !== undefined)
101+
this._cache.clear();
102+
}
103+
104+
_invalidateOrderedCache() {
105+
if(this._cacheOrderIndependent !== undefined)
106+
this._cacheOrderIndependent.clear();
107+
}
108+
}
109+
110+
module.exports = SortableSet;

0 commit comments

Comments
 (0)