mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
Pre-allocate slice in generic Map function
This commit is contained in:
+3
-1
@@ -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
|
// Map is the generic version of Selection.Map, allowing any type to be
|
||||||
// returned.
|
// returned.
|
||||||
func Map[E any](s *Selection, f func(int, *Selection) E) (result []E) {
|
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 {
|
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
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user