Skip to content

Commit bac103f

Browse files
committed
Convert the RefSet primitive to a proper class and use a Set interally
The `RefSet` primitive predates ES6, so that most likely explains why an object is used internally to track the entries. However, nowadays we can use built-in JavaScript sets for this purpose. Built-in types are often more efficient/optimized and using it makes the code a bit more clear since we don't have to assign `true` to keys anymore just to indicate their presence.
1 parent c97200f commit bac103f

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/core/primitives.js

+13-18
Original file line numberDiff line numberDiff line change
@@ -221,28 +221,23 @@ var Ref = (function RefClosure() {
221221

222222
// The reference is identified by number and generation.
223223
// This structure stores only one instance of the reference.
224-
var RefSet = (function RefSetClosure() {
225-
// eslint-disable-next-line no-shadow
226-
function RefSet() {
227-
this.dict = Object.create(null);
224+
class RefSet {
225+
constructor() {
226+
this._set = new Set();
228227
}
229228

230-
RefSet.prototype = {
231-
has: function RefSet_has(ref) {
232-
return ref.toString() in this.dict;
233-
},
234-
235-
put: function RefSet_put(ref) {
236-
this.dict[ref.toString()] = true;
237-
},
229+
has(ref) {
230+
return this._set.has(ref.toString());
231+
}
238232

239-
remove: function RefSet_remove(ref) {
240-
delete this.dict[ref.toString()];
241-
},
242-
};
233+
put(ref) {
234+
this._set.add(ref.toString());
235+
}
243236

244-
return RefSet;
245-
})();
237+
remove(ref) {
238+
this._set.delete(ref.toString());
239+
}
240+
}
246241

247242
var RefSetCache = (function RefSetCacheClosure() {
248243
// eslint-disable-next-line no-shadow

0 commit comments

Comments
 (0)