mirror of
https://github.com/servo/servo.git
synced 2025-10-01 00:59:15 +01:00
Caching HTMCollections.
We cache the state of any live HTMLCollection, keeping track of a) the optional cached length of the collection, and b) an optional cursor into the collection (a node in the collection plus its index). The cache is invalidated based on the version number of the node. We use these caches for speeding up random access to the collection. When returning coll[i], we search from the cursor, if it exists, and otherwise search from the front of the collection. In particular, both a forward for-loop and a backward for-loop through the collection will now have each access take O(1) time rather than O(n) time. This gets 1000x speed-up on the relevant Dromaeo DOM query tests.
This commit is contained in:
parent
64a50bcf56
commit
237ddc3c0c
3 changed files with 291 additions and 78 deletions
|
@ -52,13 +52,68 @@
|
|||
|
||||
var new_live = document.createElement("div");
|
||||
new_live.className = "live";
|
||||
assert_equals(htmlcollection.length, 1);
|
||||
|
||||
document.body.appendChild(new_live);
|
||||
assert_equals(htmlcollection.length, 2);
|
||||
assert_equals(htmlcollection.item(1), new_live);
|
||||
|
||||
new_live.className = "dead";
|
||||
assert_equals(htmlcollection.length, 1);
|
||||
|
||||
new_live.className = "live";
|
||||
assert_equals(htmlcollection.length, 2);
|
||||
|
||||
document.body.removeChild(new_live);
|
||||
assert_equals(htmlcollection.length, 1);
|
||||
}, "live HTMLCollection");
|
||||
}, "live HTMLCollection byClassName");
|
||||
|
||||
test(function() {
|
||||
var element = document.createElement("div");
|
||||
var coll = element.getElementsByTagName("div");
|
||||
assert_equals(coll.length, 0);
|
||||
|
||||
element.innerHTML = '<div id="q1"><span id="q2">a</span><div id="q3">b</div><div id="q4">c</div></div>';
|
||||
assert_equals(coll.length, 3);
|
||||
|
||||
var child = coll[0];
|
||||
var grandchild = coll[1];
|
||||
assert_equals(child.id, "q1");
|
||||
assert_equals(grandchild.id, "q3");
|
||||
assert_equals(grandchild.parentNode, child);
|
||||
assert_equals(child.parentNode, element);
|
||||
|
||||
child.removeChild(grandchild);
|
||||
assert_equals(coll.length, 2);
|
||||
|
||||
child.appendChild(grandchild);
|
||||
assert_equals(coll.length, 3);
|
||||
}, "live HTMLCollection byTagName");
|
||||
|
||||
test(function() {
|
||||
var element = document.createElement("div");
|
||||
var coll = element.getElementsByTagName("div");
|
||||
assert_equals(coll.length, 0);
|
||||
|
||||
element.innerHTML = '<div id="n0"><div id="n1"><div id="n2"></div><div id="n3"></div></div></div>';
|
||||
assert_equals(coll.length, 4);
|
||||
|
||||
assert_equals(coll[3].id, "n3");
|
||||
assert_equals(coll[2].id, "n2");
|
||||
assert_equals(coll[2].id, "n2");
|
||||
assert_equals(coll[1].id, "n1");
|
||||
assert_equals(coll[2].id, "n2");
|
||||
assert_equals(coll[0].id, "n0");
|
||||
|
||||
assert_equals(coll[0].id, "n0");
|
||||
assert_equals(coll[2].id, "n2");
|
||||
assert_equals(coll[1].id, "n1");
|
||||
assert_equals(coll[2].id, "n2");
|
||||
assert_equals(coll[3].id, "n3");
|
||||
|
||||
assert_equals(coll.length, 4);
|
||||
|
||||
}, "HTMLCollection cursoring");
|
||||
|
||||
test(function() {
|
||||
assert_equals(document.getElementsByTagName("DIV").length, 5);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue