Skip to content

Instantly share code, notes, and snippets.

@mattatcha
Last active August 29, 2015 14:02
Show Gist options
  • Save mattatcha/f4c3c70a063e2fc81017 to your computer and use it in GitHub Desktop.
Save mattatcha/f4c3c70a063e2fc81017 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/idada/v8.go"
"io/ioutil"
)
func check(a string) {
fmt.Println(a)
}
func main() {
engine := v8.NewEngine()
global := engine.NewObjectTemplate()
global.Bind("check", check)
context := engine.NewContext(global)
context.Scope(func(cs v8.ContextScope) {
lo, err := ioutil.ReadFile("lodash.js")
if err != nil {
return
}
lodash := engine.Compile(lo, nil)
cs.Run(lodash)
ben, err := ioutil.ReadFile("benchmark.js")
if err != nil {
return
}
benchmark := engine.Compile(ben, nil)
cs.Run(benchmark)
testScript := []byte(`
check("test");
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function() {
var itemCount = 1000000;
};
suite.add('single push', '\
var result = [];\n\
for(var i=0; i \x3C itemCount; i++) {\n\
result.push(i);\n\
}'
)
.add('2 push ', '\
var result = [];\n\
var count = itemCount/2\n\
for(var i=0; i \x3C count; i++) {\n\
result.push(i*2+0);\n\
result.push(i*2+1);\n\
}'
)
.add('2 push use base', '\
var result = [];\n\
var count = itemCount/2\n\
for(var i=0; i \x3C count; i++) {\n\
var base = i*2;\n\
result.push(base+0);\n\
result.push(base+1);\n\
}'
)
.add('5 push', '\
var result = [];\n\
var count = itemCount/5\n\
for(var i=0; i \x3C count; i++) {\n\
result.push(i*5+0);\n\
result.push(i*5+1);\n\
result.push(i*5+2);\n\
result.push(i*5+3);\n\
result.push(i*5+4);\n\
}'
)
.add('5 push use base', '\
var result = [];\n\
var count = itemCount/5\n\
for(var i=0; i \x3C count; i++) {\n\
var base = i*5;\n\
result.push(base+0);\n\
result.push(base+1);\n\
result.push(base+2);\n\
result.push(base+3);\n\
result.push(base+4);\n\
}'
)
.add('10 push', '\
var result = [];\n\
var count = itemCount/10\n\
for(var i=0; i \x3C count; i++) {\n\
result.push(i*10+0);\n\
result.push(i*10+1);\n\
result.push(i*10+2);\n\
result.push(i*10+3);\n\
result.push(i*10+4);\n\
result.push(i*10+5);\n\
result.push(i*10+6);\n\
result.push(i*10+7);\n\
result.push(i*10+8);\n\
result.push(i*10+9);\n\
}'
)
.add('10 push use base', '\
var result = [];\n\
var count = itemCount/10\n\
for(var i=0; i \x3C count; i++) {\n\
var base = i*10;\n\
result.push(base+0);\n\
result.push(base+1);\n\
result.push(base+2);\n\
result.push(base+3);\n\
result.push(base+4);\n\
result.push(base+5);\n\
result.push(base+6);\n\
result.push(base+7);\n\
result.push(base+8);\n\
result.push(base+9);\n\
}'
)
// add listeners
.on('cycle', function(event) {
check(String(event.target));
})
.on('complete', function() {
check('Fastest is ' + this.filter('fastest').pluck('name'));
})
// run async
.run({ 'async': true });
`)
test := engine.Compile(testScript, nil)
cs.Run(test)
})
}
➜ v8-go-test node nodetest.js
hasownproperty x 20,284,333 ops/sec ±1.61% (85 runs sampled)
undefined x 162,378,112 ops/sec ±29.83% (47 runs sampled)
Fastest is undefined
➜ v8-go-test go run main.go
test
hasownproperty x 14,973,628 ops/sec ?0.68% (67 runs sampled)
undefined x 449,704,141 ops/sec ?0.56% (69 runs sampled)
Fastest is undefined
➜ v8-go-test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment