diff --git a/traversal.go b/traversal.go index c495eea..09b0b66 100644 --- a/traversal.go +++ b/traversal.go @@ -657,6 +657,20 @@ func getChildrenWithSiblingType(parent *html.Node, st siblingType, skipNode *htm } } + // For the collect-all cases, count the results first so result can be + // presized, avoiding append's repeated reallocations. + switch st { + case siblingAll, siblingAllIncludingNonElements, siblingPrevAll, siblingNextAll: + n := 0 + for c := iter(nil); c != nil; c = iter(c) { + n++ + } + if n == 0 { + return nil + } + result = make([]*html.Node, 0, n) + } + for c := iter(nil); c != nil; c = iter(c) { // If this is an ...Until case, test before append (returns true // if the until condition is reached)