Skip to content

Commit 2095230

Browse files
authored
Merge pull request #469 from jwilsson/pre-allocate-slice-in-map
Pre-allocate slice in generic Map function
2 parents 4cbe087 + ce7330a commit 2095230

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

iteration.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ func (s *Selection) Map(f func(int, *Selection) string) (result []string) {
3737
// Map is the generic version of Selection.Map, allowing any type to be
3838
// returned.
3939
func Map[E any](s *Selection, f func(int, *Selection) E) (result []E) {
40+
result = make([]E, len(s.Nodes))
41+
4042
for i, n := range s.Nodes {
41-
result = append(result, f(i, newSingleSelection(n, s.document)))
43+
result[i] = f(i, newSingleSelection(n, s.document))
4244
}
4345

4446
return result

0 commit comments

Comments
 (0)