Skip to content

Commit

Permalink
Use Set instead of Array for HTMLCollection keys
Browse files Browse the repository at this point in the history
This significantly improves performance of parsing large documents.
  • Loading branch information
Zirro authored and domenic committed May 20, 2017
1 parent f10e89d commit 1833f78
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/jsdom/living/html-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class HTMLCollection {
throw new TypeError("Invalid constructor");
}

this[privates] = { element, query, keys: [], length: 0, version: -1, conflictElements: Object.create(null) };
this[privates] = { element, query, keys: new Set(), length: 0, version: -1, conflictElements: Object.create(null) };
updateHTMLCollection(this);
}

Expand Down Expand Up @@ -70,10 +70,10 @@ function resetHTMLCollectionTo(collection, impls) {
collection[privates].length = wrappers.length;

const keys = collection[privates].keys;
for (let i = 0; i < keys.length; ++i) {
for (let i = 0; i < keys.size; ++i) {
delete collection[keys[i]];
}
keys.length = 0;
keys.clear();

for (let i = 0; i < impls.length; ++i) {
addIfAttrPresent(impls[i], wrappers[i], "name");
Expand All @@ -96,7 +96,7 @@ function resetHTMLCollectionTo(collection, impls) {
}

// Don't override existing named ones
if (keys.includes(value)) {
if (keys.has(value)) {
return;
}

Expand All @@ -105,7 +105,7 @@ function resetHTMLCollectionTo(collection, impls) {
} else {
collection[value] = wrapper;
}
keys.push(value);
keys.add(value);
}
}

Expand Down

0 comments on commit 1833f78

Please sign in to comment.