all repos — gomponents @ ac5c615468e972376d23447de91eb1ea39a315bb

HTML components in pure Go

Set slice capacity based on input in `Map` and `Classes.Render` (#243) Hey! I just discovered this repo and I love being able to write templates while still having compile-time checks. I was looking through the code and noticed slices aren't currently preallocated in `Map()` and `Classes.Render()`. Preallocating will improve performance since the resulting slice won't have to be grown dynamically during the append loop.

Gabe Cook
commit

ac5c615468e972376d23447de91eb1ea39a315bb

parent

0368e39c137c7f1ab37c84c0787fd6cc9da0c34b

2 files changed, 2 insertions(+), 2 deletions(-)

changed files
M components/components.gocomponents/components.go
@@ -44,7 +44,7 @@ type Classes map[string]bool
// Render satisfies [g.Node]. func (c Classes) Render(w io.Writer) error { - var included []string + included := make([]string, 0, len(c)) for c, include := range c { if include { included = append(included, c)
M gomponents.gogomponents.go
@@ -250,7 +250,7 @@ }
// Map a slice of anything to a [Group] (which is just a slice of [Node]-s). func Map[T any](ts []T, cb func(T) Node) Group { - var nodes []Node + nodes := make([]Node, 0, len(ts)) for _, t := range ts { nodes = append(nodes, cb(t)) }