Skip to content

Commit 3951e60

Browse files
author
Saddam Azy
committed
increase performance by migrating from bytes.Buffer to strings.Builder
1 parent e8b5d74 commit 3951e60

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

bench_example_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package goquery
22

33
import (
4-
"bytes"
54
"fmt"
65
"strconv"
6+
"strings"
77
"testing"
88
)
99

1010
func BenchmarkMetalReviewExample(b *testing.B) {
1111
var n int
12-
var buf bytes.Buffer
12+
var builder strings.Builder
1313

1414
b.StopTimer()
1515
doc := loadDoc("metalreview.html")
@@ -27,12 +27,12 @@ func BenchmarkMetalReviewExample(b *testing.B) {
2727
if score, e = strconv.ParseFloat(s.Find(".score").Text(), 64); e != nil {
2828
// Not a valid float, ignore score
2929
if n <= 4 {
30-
buf.WriteString(fmt.Sprintf("Review %d: %s - %s.\n", i, band, title))
30+
builder.WriteString(fmt.Sprintf("Review %d: %s - %s.\n", i, band, title))
3131
}
3232
} else {
3333
// Print all, including score
3434
if n <= 4 {
35-
buf.WriteString(fmt.Sprintf("Review %d: %s - %s (%2.1f).\n", i, band, title, score))
35+
builder.WriteString(fmt.Sprintf("Review %d: %s - %s (%2.1f).\n", i, band, title, score))
3636
}
3737
}
3838
})

property.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package goquery
22

33
import (
4-
"bytes"
54
"regexp"
65
"strings"
76

@@ -60,14 +59,14 @@ func (s *Selection) SetAttr(attrName, val string) *Selection {
6059
// Text gets the combined text contents of each element in the set of matched
6160
// elements, including their descendants.
6261
func (s *Selection) Text() string {
63-
var buf bytes.Buffer
62+
var builder strings.Builder
6463

6564
// Slightly optimized vs calling Each: no single selection object created
6665
var f func(*html.Node)
6766
f = func(n *html.Node) {
6867
if n.Type == html.TextNode {
6968
// Keep newlines and spaces, like jQuery
70-
buf.WriteString(n.Data)
69+
builder.WriteString(n.Data)
7170
}
7271
if n.FirstChild != nil {
7372
for c := n.FirstChild; c != nil; c = c.NextSibling {
@@ -79,7 +78,7 @@ func (s *Selection) Text() string {
7978
f(n)
8079
}
8180

82-
return buf.String()
81+
return builder.String()
8382
}
8483

8584
// Size is an alias for Length.
@@ -97,16 +96,16 @@ func (s *Selection) Length() int {
9796
func (s *Selection) Html() (ret string, e error) {
9897
// Since there is no .innerHtml, the HTML content must be re-created from
9998
// the nodes using html.Render.
100-
var buf bytes.Buffer
99+
var builder strings.Builder
101100

102101
if len(s.Nodes) > 0 {
103102
for c := s.Nodes[0].FirstChild; c != nil; c = c.NextSibling {
104-
e = html.Render(&buf, c)
103+
e = html.Render(&builder, c)
105104
if e != nil {
106105
return
107106
}
108107
}
109-
ret = buf.String()
108+
ret = builder.String()
110109
}
111110

112111
return

utilities.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package goquery
22

33
import (
4-
"bytes"
54
"io"
5+
"strings"
66

77
"golang.org/x/net/html"
88
)
@@ -26,13 +26,12 @@ var nodeNames = []string{
2626
// Go's net/html package defines the following node types, listed with
2727
// the corresponding returned value from this function:
2828
//
29-
// ErrorNode : #error
30-
// TextNode : #text
31-
// DocumentNode : #document
32-
// ElementNode : the element's tag name
33-
// CommentNode : #comment
34-
// DoctypeNode : the name of the document type
35-
//
29+
// ErrorNode : #error
30+
// TextNode : #text
31+
// DocumentNode : #document
32+
// ElementNode : the element's tag name
33+
// CommentNode : #comment
34+
// DoctypeNode : the name of the document type
3635
func NodeName(s *Selection) string {
3736
if s.Length() == 0 {
3837
return ""
@@ -77,11 +76,11 @@ func Render(w io.Writer, s *Selection) error {
7776
// because this is not a jQuery method (in javascript-land, this is
7877
// a property provided by the DOM).
7978
func OuterHtml(s *Selection) (string, error) {
80-
var buf bytes.Buffer
81-
if err := Render(&buf, s); err != nil {
79+
var builder strings.Builder
80+
if err := Render(&builder, s); err != nil {
8281
return "", err
8382
}
84-
return buf.String(), nil
83+
return builder.String(), nil
8584
}
8685

8786
// Loop through all container nodes to search for the target node.

0 commit comments

Comments
 (0)