Skip to content

Instantly share code, notes, and snippets.

@hajimehoshi
Last active June 23, 2018 18:59
Show Gist options
  • Save hajimehoshi/6fe92106b1adc0ec8d4cf48cf7e4e509 to your computer and use it in GitHub Desktop.
Save hajimehoshi/6fe92106b1adc0ec8d4cf48cf7e4e509 to your computer and use it in GitHub Desktop.
GopherJS vs Wasm
package main
func array() []float32 {
return make([]float32, 16)
}
func heavyTask() float32 {
arr := array()
result := float32(0.0)
for i := 0; i < 10000000; i++ {
u0, v0, u1, v1 := float32(0.0), float32(0.0), float32(1.0), float32(1.0)
arr[0] = u0
arr[1] = v0
arr[2] = u1
arr[3] = u1
arr[4] = u1
arr[5] = v0
arr[6] = u0
arr[7] = u1
arr[8] = u0
arr[9] = v1
arr[10] = u1
arr[11] = u0
arr[12] = u1
arr[13] = v1
arr[14] = u0
arr[15] = u0
result += arr[i % 16]
}
return result
}
func main() {
println(heavyTask())
}
function array() {
return new Float32Array(16);
}
function heavyTask() {
const arr = array();
result = 0.0;
for (let i = 0; i < 10000000; i++) {
const u0 = 0.0;
const v0 = 0.0;
const u1 = 1.0;
const v1 = 1.0;
arr[0] = u0
arr[1] = v0
arr[2] = u1
arr[3] = u1
arr[4] = u1
arr[5] = v0
arr[6] = u0
arr[7] = u1
arr[8] = u0
arr[9] = v1
arr[10] = u1
arr[11] = u0
arr[12] = u1
arr[13] = v1
arr[14] = u0
arr[15] = u0
result += arr[i % 16]
}
return result;
}
function main() {
console.log(heavyTask());
}
main();
$ # Go -> Wasm
$ GOOS=js GOARCH=wasm ~/go-code/bin/go build -o test.wasm .
$ time ./go_js_wasm_exec test.wasm
+5.000000e+006
real 0m0.740s
user 0m1.480s
sys 0m0.066s
$ # GopherJS
$ gopherjs build -o test.js main.go
$ time node test.js
5000000
real 0m0.268s
user 0m0.236s
sys 0m0.024s
$ # Plain JS
$ time node main.js
5000000
real 0m0.309s
user 0m0.142s
sys 0m0.043s
@bketelsen
Copy link

that's a big difference

@hajimehoshi
Copy link
Author

Right, significantly different...

@egonelbre
Copy link

How much of that is the heavy task itself?

@hajimehoshi
Copy link
Author

Good point. I'll add time.Now before and after heavyTask() call.

@hajimehoshi
Copy link
Author

https://gist.github.com/hajimehoshi/f9299abc0890283dfe803ed80767f5b5 Here you are: this takes wall-time clock before and after the heavy task.

@alexdor
Copy link

alexdor commented Jun 23, 2018

Which browser are you using to test wasm?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment