Skip to content

Commit 469a6ef

Browse files
Warm up the index cache if childrenToArray is used
1 parent 881ec18 commit 469a6ef

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/SymbolTree.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,18 @@ class SymbolTree {
256256
}
257257

258258
let object = this._node(parent).first;
259+
let index = 0;
259260

260261
while (object) {
262+
const node = this._node(object);
263+
node.setCachedIndex(parent, index);
264+
261265
if (filter.call(thisArg, object)) {
262266
array.push(object);
263267
}
264-
object = this._node(object).next;
268+
269+
object = node.next;
270+
++index;
265271
}
266272

267273
return array;

test/SymbolTree.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,29 @@ test('cached index', function(t) {
961961
t.end();
962962
});
963963

964+
test('cached index warmed up by childrenToArray', function(t) {
965+
const tree = new SymbolTree();
966+
const a = {};
967+
const aa = {};
968+
const ab = {};
969+
const aba = {};
970+
const ac = {};
971+
const b = {};
972+
973+
tree.insertLast(aa, a);
974+
tree.insertLast(ab, a);
975+
tree.insertLast(aba, ab);
976+
tree.insertLast(ac, a);
977+
tree.insertAfter(b, a);
978+
979+
tree.childrenToArray(a);
980+
t.equal(0, tree.index(aa));
981+
t.equal(1, tree.index(ab));
982+
t.equal(2, tree.index(ac));
983+
984+
t.end();
985+
});
986+
964987
test('children count', function(t) {
965988
// no need to test the caching since we already tested for that in childrenCount
966989
const tree = new SymbolTree();

0 commit comments

Comments
 (0)