From ce7330a252a3455c06be6842aeb502a0c204bad6 Mon Sep 17 00:00:00 2001 From: Jonathan Wilsson Date: Wed, 28 Feb 2024 21:02:30 +0100 Subject: [PATCH] Pre-allocate slice in generic Map function --- iteration.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/iteration.go b/iteration.go index 160fa90..1c98158 100644 --- a/iteration.go +++ b/iteration.go @@ -37,8 +37,10 @@ func (s *Selection) Map(f func(int, *Selection) string) (result []string) { // Map is the generic version of Selection.Map, allowing any type to be // returned. func Map[E any](s *Selection, f func(int, *Selection) E) (result []E) { + result = make([]E, len(s.Nodes)) + for i, n := range s.Nodes { - result = append(result, f(i, newSingleSelection(n, s.document))) + result[i] = f(i, newSingleSelection(n, s.document)) } return result