diff options
author | Markus Wüstenberg | 2020-10-29 15:40:14 +0100 |
---|---|---|
committer | GitHub | 2020-10-29 15:40:14 +0100 |
commit | 92ba5904c1645e6572f5ff1b9d0e0ec629e1afb9 (patch) | |
tree | a8152ee9fae45f724faf429c0bbd14c2ba62f15b /gomponents_test.go | |
parent | f277d1942e9f2ec1ff4a3bc3c409e864103b506b (diff) | |
download | gomponents-92ba5904c1645e6572f5ff1b9d0e0ec629e1afb9.tar.lz gomponents-92ba5904c1645e6572f5ff1b9d0e0ec629e1afb9.tar.zst gomponents-92ba5904c1645e6572f5ff1b9d0e0ec629e1afb9.zip |
Remove fmt.Sprintf call in attribute Render (#38)
Just concatenating the strings is much faster. Before: ``` make benchmark go test -bench=. goos: darwin goarch: amd64 pkg: github.com/maragudk/gomponents BenchmarkAttr/boolean_attributes-8 8194791 139 ns/op BenchmarkAttr/name-value_attributes-8 5143292 229 ns/op PASS ok github.com/maragudk/gomponents 2.841s ``` After: ``` make benchmark go test -bench=. goos: darwin goarch: amd64 pkg: github.com/maragudk/gomponents BenchmarkAttr/boolean_attributes-8 16755404 67.0 ns/op BenchmarkAttr/name-value_attributes-8 10208625 116 ns/op PASS ok github.com/maragudk/gomponents 2.702s ```
Diffstat (limited to 'gomponents_test.go')
-rw-r--r-- | gomponents_test.go | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/gomponents_test.go b/gomponents_test.go index 33d32d1..2efdd7a 100644 --- a/gomponents_test.go +++ b/gomponents_test.go @@ -52,6 +52,22 @@ func TestAttr(t *testing.T) { }) } +func BenchmarkAttr(b *testing.B) { + b.Run("boolean attributes", func(b *testing.B) { + for i := 0; i < b.N; i++ { + a := g.Attr("hat") + a.Render() + } + }) + + b.Run("name-value attributes", func(b *testing.B) { + for i := 0; i < b.N; i++ { + a := g.Attr("hat", "party") + a.Render() + } + }) +} + type outsider struct{} func (o outsider) Render() string { |