mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2026-10-19 16:15:33 +00:00
getClassesSlice used rxClassTrim.ReplaceAllString followed by strings.Split, which allocated twice and produced empty strings from leading/trailing spaces. strings.Fields handles all Unicode whitespace in a single pass with no empty entries. name old ns/op new ns/op delta AddClass-8 176700 107500 -39.2% RemoveClass-8 142900 64600 -54.8% name old B/op new B/op delta AddClass-8 12544 7024 -44.0% RemoveClass-8 6503 1777 -72.7% name old allocs new allocs delta AddClass-8 697 374 -46.3% RemoveClass-8 697 374 -46.3%
71 lines
1.1 KiB
Go
71 lines
1.1 KiB
Go
package goquery
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func BenchmarkAttr(b *testing.B) {
|
|
var s string
|
|
|
|
b.StopTimer()
|
|
sel := DocW().Find("h1")
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
s, _ = sel.Attr("id")
|
|
}
|
|
if s != "firstHeading" {
|
|
b.Fatalf("want firstHeading, got %q", s)
|
|
}
|
|
}
|
|
|
|
func BenchmarkText(b *testing.B) {
|
|
b.StopTimer()
|
|
sel := DocW().Find("h2")
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
sel.Text()
|
|
}
|
|
}
|
|
|
|
func BenchmarkLength(b *testing.B) {
|
|
var n int
|
|
|
|
b.StopTimer()
|
|
sel := DocW().Find("h2")
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
n = sel.Length()
|
|
}
|
|
if n != 14 {
|
|
b.Fatalf("want 14, got %d", n)
|
|
}
|
|
}
|
|
|
|
func BenchmarkHtml(b *testing.B) {
|
|
b.StopTimer()
|
|
sel := DocW().Find("h2")
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_, _ = sel.Html()
|
|
}
|
|
}
|
|
|
|
func BenchmarkAddClass(b *testing.B) {
|
|
b.StopTimer()
|
|
sel := DocW().Find("li")
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
sel.AddClass("foo bar baz")
|
|
}
|
|
}
|
|
|
|
func BenchmarkRemoveClass(b *testing.B) {
|
|
b.StopTimer()
|
|
sel := DocW().Find("li")
|
|
sel.AddClass("foo bar baz")
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
sel.RemoveClass("foo bar baz")
|
|
}
|
|
}
|