Convert RefSetCache
to a proper class and to use a Map
internally
Using a `Map` instead of an `Object` provides some advantages such as cheaper ways to get the size of the cache, to find out if an entry is contained in the cache and to iterate over the cache. Moreover, we can clear and re-use the same `Map` object now instead of creating a new one.
This commit is contained in:
parent
29adbb7cd7
commit
b19a1796ac
@ -239,46 +239,41 @@ class RefSet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var RefSetCache = (function RefSetCacheClosure() {
|
class RefSetCache {
|
||||||
// eslint-disable-next-line no-shadow
|
constructor() {
|
||||||
function RefSetCache() {
|
this._map = new Map();
|
||||||
this.dict = Object.create(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RefSetCache.prototype = {
|
get size() {
|
||||||
get size() {
|
return this._map.size;
|
||||||
return Object.keys(this.dict).length;
|
}
|
||||||
},
|
|
||||||
|
|
||||||
get: function RefSetCache_get(ref) {
|
get(ref) {
|
||||||
return this.dict[ref.toString()];
|
return this._map.get(ref.toString());
|
||||||
},
|
}
|
||||||
|
|
||||||
has: function RefSetCache_has(ref) {
|
has(ref) {
|
||||||
return ref.toString() in this.dict;
|
return this._map.has(ref.toString());
|
||||||
},
|
}
|
||||||
|
|
||||||
put: function RefSetCache_put(ref, obj) {
|
put(ref, obj) {
|
||||||
this.dict[ref.toString()] = obj;
|
this._map.set(ref.toString(), obj);
|
||||||
},
|
}
|
||||||
|
|
||||||
putAlias: function RefSetCache_putAlias(ref, aliasRef) {
|
putAlias(ref, aliasRef) {
|
||||||
this.dict[ref.toString()] = this.get(aliasRef);
|
this._map.set(ref.toString(), this.get(aliasRef));
|
||||||
},
|
}
|
||||||
|
|
||||||
forEach: function RefSetCache_forEach(callback) {
|
forEach(callback) {
|
||||||
for (const i in this.dict) {
|
for (const value of this._map.values()) {
|
||||||
callback(this.dict[i]);
|
callback(value);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
clear: function RefSetCache_clear() {
|
clear() {
|
||||||
this.dict = Object.create(null);
|
this._map.clear();
|
||||||
},
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
return RefSetCache;
|
|
||||||
})();
|
|
||||||
|
|
||||||
function isEOF(v) {
|
function isEOF(v) {
|
||||||
return v === EOF;
|
return v === EOF;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user