Skip to content

Commit 1d81fad

Browse files
committed
Presize arrays to save heap pressure.
1 parent 3cb4858 commit 1d81fad

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

docs/lib/paged.browser.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2611,7 +2611,17 @@
26112611
// dicts at their own init sites. `findRef` does `arr[ref]` either
26122612
// way -- V8 coerces the decimal-string ref to an array index
26132613
// transparently, so no caller-side branch is needed.
2614-
if (!content.indexOfRefs) content.indexOfRefs = [];
2614+
//
2615+
// [PATCH: source-indexOfRefs-presize] Size the array up front
2616+
// from the live HTMLCollection's .length. V8 grows arrays
2617+
// geometrically -- writing slots 1..N via doubling does
2618+
// log2(N) backing-store reallocations, each allocating the
2619+
// new store and orphaning the old (transient bytes ~= 2x the
2620+
// final size). Pre-sizing skips all of that.
2621+
if (!content.indexOfRefs) {
2622+
const elementCount = content.getElementsByTagName ? content.getElementsByTagName("*").length : 0;
2623+
content.indexOfRefs = new Array(elementCount + 1);
2624+
}
26152625

26162626
let node = treeWalker.nextNode();
26172627
while(node) {

0 commit comments

Comments
 (0)