全功能模板引擎
预编译为Go代码
比较基准
DirectBuffer-使用go手动将HTML写入缓冲区,并使用基本转义
转换为Go模板
Damsel我不会对转译引擎进行基准测试,因为转译应该在启动时只发生一次。如果缓存转译结果(这是推荐的),则渲染时的性能数字将与html/template相同。
如何运行基准测试
最低配置,但没有意义,因为它会比较相同的2个go版本。
./bench.sh -c go
测试您的设置-减少测试运行时,并假设您的系统上有这两个go二进制文件。
./bench.sh -t 10ms -c go1.18 -g go1.19
在我的系统上使用每个模板引擎的默认3s运行时运行的正常基准测试示例
./bench.sh -c go1.18.6 -g go1.19.1
测试机器机器
local desktop: ryzen 3900x
简单基准测试
全功能模板引擎
comparing: go1.22.7 to go1.23.1
name old time/op new time/op delta
Golang-24 8.56µs ± 0% 7.88µs ± 0% -7.91%
GolangText-24 2.44µs ± 0% 2.37µs ± 0% -3.11%
Ace-24 13.1µs ± 0% 12.6µs ± 0% -3.65%
Amber-24 8.90µs ± 0% 8.57µs ± 0% -3.70%
Mustache-24 4.27µs ± 0% 4.38µs ± 0% +2.50%
Pongo2-24 5.72µs ± 0% 5.79µs ± 0% +1.24%
Handlebars-24 13.1µs ± 0% 13.0µs ± 0% -0.21%
Soy-24 3.47µs ± 0% 3.51µs ± 0% +1.18%
JetHTML-24 769ns ± 0% 773ns ± 0% +0.52%
name old alloc/op new alloc/op delta
Golang-24 769B ± 0% 769B ± 0% 0.00%
GolangText-24 128B ± 0% 128B ± 0% 0.00%
Ace-24 1.12kB ± 0% 1.12kB ± 0% 0.00%
Amber-24 849B ± 0% 849B ± 0% 0.00%
Mustache-24 1.72kB ± 0% 1.72kB ± 0% 0.00%
Pongo2-24 2.08kB ± 0% 2.08kB ± 0% 0.00%
Handlebars-24 3.42kB ± 0% 3.42kB ± 0% 0.00%
Soy-24 1.22kB ± 0% 1.22kB ± 0% 0.00%
JetHTML-24 0.00B 0.00B 0.00%
name old allocs/op new allocs/op delta
Golang-24 35.0 ± 0% 35.0 ± 0% 0.00%
GolangText-24 7.00 ± 0% 7.00 ± 0% 0.00%
Ace-24 40.0 ± 0% 40.0 ± 0% 0.00%
Amber-24 36.0 ± 0% 36.0 ± 0% 0.00%
Mustache-24 30.0 ± 0% 30.0 ± 0% 0.00%
Pongo2-24 32.0 ± 0% 32.0 ± 0% 0.00%
Handlebars-24 75.0 ± 0% 75.0 ± 0% 0.00%
Soy-24 19.0 ± 0% 19.0 ± 0% 0.00%
JetHTML-24 0.00 0.00 0.00%
预编译为Go代码
comparing: go1.22.7 to go1.23.1
name old time/op new time/op delta
Ego-24 1.00µs ± 0% 1.01µs ± 0% +1.60%
HB-24 7.48µs ± 0% 7.53µs ± 0% +0.72%
Quicktemplate-24 176ns ± 0% 180ns ± 0% +2.45%
Ftmpl-24 1.66µs ± 0% 1.61µs ± 0% -3.31%
Gorazor-24 829ns ± 0% 820ns ± 0% -1.13%
Hero-24 118ns ± 0% 120ns ± 0% +1.52%
Jade-24 87.0ns ± 0% 85.2ns ± 0% -1.98%
Templ-24 543ns ± 0% 557ns ± 0% +2.73%
Gomponents-24 4.87µs ± 0% 4.88µs ± 0% +0.02%
name old alloc/op new alloc/op delta
Ego-24 85.0B ± 0% 85.0B ± 0% 0.00%
HB-24 2.45kB ± 0% 2.45kB ± 0% 0.00%
Quicktemplate-24 0.00B 0.00B 0.00%
Ftmpl-24 774B ± 0% 774B ± 0% 0.00%
Gorazor-24 512B ± 0% 512B ± 0% 0.00%
Hero-24 0.00B 0.00B 0.00%
Jade-24 0.00B 0.00B 0.00%
Templ-24 96.0B ± 0% 96.0B ± 0% 0.00%
Gomponents-24 1.11kB ± 0% 1.11kB ± 0% 0.00%
name old allocs/op new allocs/op delta
Ego-24 8.00 ± 0% 8.00 ± 0% 0.00%
HB-24 51.0 ± 0% 51.0 ± 0% 0.00%
Quicktemplate-24 0.00 0.00 0.00%
Ftmpl-24 12.0 ± 0% 12.0 ± 0% 0.00%
Gorazor-24 5.00 ± 0% 5.00 ± 0% 0.00%
Hero-24 0.00 0.00 0.00%
Jade-24 0.00 0.00 0.00%
Templ-24 2.00 ± 0% 2.00 ± 0% 0.00%
Gomponents-24 56.0 ± 0% 56.0 ± 0% 0.00%
使用模板继承进行更复杂的测试(如果可能的话)
全功能模板引擎
comparing: go1.22.7 to go1.23.1
name old time/op new time/op delta
ComplexGolang-24 69.4µs ± 0% 68.8µs ± 0% -0.95%
ComplexGolangText-24 29.0µs ± 0% 30.1µs ± 0% +3.86%
ComplexMustache-24 25.7µs ± 0% 26.2µs ± 0% +1.99%
ComplexJetHTML-24 12.2µs ± 0% 12.3µs ± 0% +0.52%
name old alloc/op new alloc/op delta
ComplexGolang-24 6.57kB ± 0% 6.57kB ± 0% 0.00%
ComplexGolangText-24 2.24kB ± 0% 2.24kB ± 0% 0.00%
ComplexMustache-24 7.28kB ± 0% 7.28kB ± 0% +0.01%
ComplexJetHTML-24 534B ± 0% 535B ± 0% +0.19%
name old allocs/op new allocs/op delta
ComplexGolang-24 290 ± 0% 290 ± 0% 0.00%
ComplexGolangText-24 107 ± 0% 107 ± 0% 0.00%
ComplexMustache-24 156 ± 0% 156 ± 0% 0.00%
ComplexJetHTML-24 5.00 ± 0% 5.00 ± 0% 0.00%
预编译为Go代码
comparing: go1.22.7 to go1.23.1
name old time/op new time/op delta
ComplexEgo-24 4.98µs ± 0% 5.03µs ± 0% +1.12%
ComplexQuicktemplate-24 1.04µs ± 0% 1.06µs ± 0% +2.60%
ComplexTempl-24 2.80µs ± 0% 2.78µs ± 0% -0.54%
ComplexFtmpl-24 7.19µs ± 0% 7.25µs ± 0% +0.86%
ComplexGorazor-24 5.47µs ± 0% 5.57µs ± 0% +1.86%
ComplexHero-24 937ns ± 0% 926ns ± 0% -1.11%
ComplexJade-24 738ns ± 0% 742ns ± 0% +0.57%
ComplexGoDirectBuffer-24 520ns ± 0% 520ns ± 0% +0.10%
name old alloc/op new alloc/op delta
ComplexEgo-24 569B ± 0% 569B ± 0% 0.00%
ComplexQuicktemplate-24 0.00B 0.00B 0.00%
ComplexTempl-24 408B ± 0% 408B ± 0% 0.00%
ComplexFtmpl-24 3.54kB ± 0% 3.54kB ± 0% 0.00%
ComplexGorazor-24 3.69kB ± 0% 3.69kB ± 0% 0.00%
ComplexHero-24 0.00B 0.00B 0.00%
ComplexJade-24 0.00B 0.00B 0.00%
ComplexGoDirectBuffer-24 0.00B 0.00B 0.00%
name old allocs/op new allocs/op delta
ComplexEgo-24 31.0 ± 0% 31.0 ± 0% 0.00%
ComplexQuicktemplate-24 0.00 0.00 0.00%
ComplexTempl-24 11.0 ± 0% 11.0 ± 0% 0.00%
ComplexFtmpl-24 38.0 ± 0% 38.0 ± 0% 0.00%
ComplexGorazor-24 24.0 ± 0% 24.0 ± 0% 0.00%
ComplexHero-24 0.00 0.00 0.00%
ComplexJade-24 0.00 0.00 0.00%
ComplexGoDirectBuffer-24 0.00 0.00 0.00%
安全
所有包都假定模板作者是受信任的。如果您允许自定义模板,则必须对用户输入进行消毒,例如bluemonay。一般来说,我建议对每个输入进行消毒,而不仅仅是HTML输入。
评论