goja
goja copied to clipboard
Performance; calling go function
I have simple code that gets strings.Builder and writes some strings in a loop. This code takes like ~20000 ns/op (equivalent in Go takes like ~500 ns/op).
Building the string itself, without calling strings.Builder, takes ~8000 ns/op.
Is there any way to optimize Go function calls (on passed objects)?
package goja_test
import (
"fmt"
"strings"
"testing"
"github.com/dop251/goja"
)
var compiled *goja.Program
func _goja_forIssue() any {
SCRIPT := `
function WriteSomething({ sb }) {
for (let i = 0; i <= 10; i++) {
const x = 'nnn' + i + ','
sb.WriteString(x)
}
}`
vm := goja.New()
if compiled == nil {
compiled, _ = goja.Compile("perf", SCRIPT, true)
}
vm.RunProgram(compiled)
writeSomething, ok := goja.AssertFunction(vm.Get("WriteSomething"))
if !ok {
panic("Not a function")
}
sb := &strings.Builder{}
prms := map[string]any{
"sb": sb,
}
writeSomething(goja.Undefined(), vm.ToValue(prms))
return sb.String()
}
func BenchmarkSimple1(b *testing.B) {
for i := 0; i < b.N; i++ {
_goja_forIssue()
}
}