-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
108 lines (95 loc) · 1.93 KB
/
bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package react
import (
"strconv"
"strings"
"testing"
)
var (
div *Element
app *Element
ela *Element
)
func init() {
div = CreateElement("div", Props{"className": "ui__container"})
app = App()
ela = CreateElement("div", nil, List(4),
CreateElement("div", nil, List(4),
CreateElement("div", nil, List(4)),
CreateElement("div", nil, List(4)),
CreateElement("div", nil, List(4),
CreateElement("div", nil, List(4),
CreateElement("div", nil, List(4)),
),
CreateElement("div", nil, List(4)),
),
),
)
}
func BenchmarkRender(b *testing.B) {
b.Run("SimpleElement", func(b *testing.B) {
for n := 0; n < b.N; n++ {
Render(div)
}
})
b.Run("SmallElement", func(b *testing.B) {
for n := 0; n < b.N; n++ {
Render(app)
}
})
b.Run("TreeElement", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = NewRenderer(ela).String()
}
})
}
func BenchmarkJoins(b *testing.B) {
var r []string
for i := 0; i < 1024; i++ {
r = append(r, "item "+strconv.Itoa(i))
}
b.Run("strings.Join", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = strings.Join(r, "")
}
})
b.Run("string loop concat", func(b *testing.B) {
for i := 0; i < b.N; i++ {
str := ""
for _, e := range r {
str += e
}
}
})
b.Run("string builder", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var str strings.Builder
for _, e := range r {
str.WriteString(e)
}
}
})
}
func BenchmarkStrings(b *testing.B) {
var rs []string
var rb [][]byte
for i := 0; i < 1024; i++ {
rs = append(rs, "item "+strconv.Itoa(i))
rb = append(rb, []byte("item "+strconv.Itoa(i)))
}
b.Run("strings", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var content strings.Builder
for j := 0; j < 1024; j++ {
content.WriteString(rs[j])
}
}
})
b.Run("bytes", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var content strings.Builder
for j := 0; j < 1024; j++ {
content.Write(rb[j])
}
}
})
}