Skip to content

Commit

Permalink
feat(gqlerror): optimize Error method perfomance (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
dienvt authored Oct 25, 2024
1 parent 6361a74 commit 89fc137
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions gqlerror/error.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package gqlerror

import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"

"github.com/vektah/gqlparser/v2/ast"
)
Expand Down Expand Up @@ -38,7 +38,7 @@ type Location struct {
type List []*Error

func (err *Error) Error() string {
var res bytes.Buffer
var res strings.Builder
if err == nil {
return ""
}
Expand Down Expand Up @@ -80,7 +80,7 @@ func (err *Error) AsError() error {
}

func (errs List) Error() string {
var buf bytes.Buffer
var buf strings.Builder
for _, err := range errs {
buf.WriteString(err.Error())
buf.WriteByte('\n')
Expand Down
11 changes: 11 additions & 0 deletions gqlerror/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/vektah/gqlparser/v2/ast"
)

Expand Down Expand Up @@ -166,3 +167,13 @@ func TestList_Is(t *testing.T) {
})
}
}

func BenchmarkError(b *testing.B) {
list := List([]*Error{error1, error2})
for i := 0; i < b.N; i++ {
_ = underlyingError.Error()
_ = error1.Error()
_ = error2.Error()
_ = list.Error()
}
}

0 comments on commit 89fc137

Please sign in to comment.