Skip to content

Commit

Permalink
Convert RefSetCache to a proper class and to use a Map internally
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
timvandermeij committed Jul 17, 2020
1 parent 29adbb7 commit b19a179
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions src/core/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,46 +239,41 @@ class RefSet {
}
}

var RefSetCache = (function RefSetCacheClosure() {
// eslint-disable-next-line no-shadow
function RefSetCache() {
this.dict = Object.create(null);
class RefSetCache {
constructor() {
this._map = new Map();
}

RefSetCache.prototype = {
get size() {
return Object.keys(this.dict).length;
},

get: function RefSetCache_get(ref) {
return this.dict[ref.toString()];
},
get size() {
return this._map.size;
}

has: function RefSetCache_has(ref) {
return ref.toString() in this.dict;
},
get(ref) {
return this._map.get(ref.toString());
}

put: function RefSetCache_put(ref, obj) {
this.dict[ref.toString()] = obj;
},
has(ref) {
return this._map.has(ref.toString());
}

putAlias: function RefSetCache_putAlias(ref, aliasRef) {
this.dict[ref.toString()] = this.get(aliasRef);
},
put(ref, obj) {
this._map.set(ref.toString(), obj);
}

forEach: function RefSetCache_forEach(callback) {
for (const i in this.dict) {
callback(this.dict[i]);
}
},
putAlias(ref, aliasRef) {
this._map.set(ref.toString(), this.get(aliasRef));
}

clear: function RefSetCache_clear() {
this.dict = Object.create(null);
},
};
forEach(callback) {
for (const value of this._map.values()) {
callback(value);
}
}

return RefSetCache;
})();
clear() {
this._map.clear();
}
}

function isEOF(v) {
return v === EOF;
Expand Down

0 comments on commit b19a179

Please sign in to comment.