Go WASM Benchmark
July 31, 2018
In the last days I have often read articles on the recent Web Assembly (Wasm) support landed in Go 1.11 (expected in August … soon). If it’s the first time you hear about web assembly I invite you to read this.
What was the first thing I thought to see its power ??
BENCHMARK obviously; let’s try the good old Fibonacci on WASM.
I implemented the function in the most optimized way I found and with some help I arrived at the following result:
GO
func fibonacci() func() *big.Int {
a := big.NewInt(0)
b := big.NewInt(1)
return func() *big.Int {
defer func() {
a.Add(a, b)
a, b = b, a
}()
return a
}
}
Javascript
function fibonacci(num){
var a = bigInt.one, b = bigInt.zero, temp;
while (num >= 0){
temp = a;
a = a.plus(b);
b = temp;
num--;
}
return b.toString();
}
Once working I introduced a method to launch both functions together with a bit of debugging, and a method to calculate the average time of a battery of ten consecutive executions.
And the first results suddently appears ..
Firefox 61.0.1(64bit) for Ubuntu
f(x) | Javascript (ms) | WASM (ms) |
---|---|---|
f(10) | 0.6 | 7.5 |
f(1000) | 2.2 | 8.4 |
f(5000) | 14.5 | 9.9 |
f(10000) | 45 | 15.7 |
f(50000) | 508.8 | 108.1 |
Chrome Version 68.0.3440.75 (Official Build) (64-bit) for Ubuntu
f(x) | Javascript (ms) | WASM (ms) |
---|---|---|
f(10) | 0.14 | 8.9 |
f(1000) | 1.20 | 9.7 |
f(5000) | 8.05 | 13.61 |
f(10000) | 19.31 | 17.21 |
f(50000) | 342.15 | 115.51 |
Here the same data on a plot
From the plot you can notice that for simple calculations the loading of the WASM library is not yet optimized and slows down the execution. The difference is no longer noted on the calculation of Fibonacci (5000) in which Wasm claims its calculation speed compared to Javascript.
In order to make it easier to read from the graph, I have omitted the Fibonacci calculation (50000) from which we can deduce total supremacy of Wasm on both browsers when the calculations become much more serious.
All the sources are avaiable on my github along with an explanation of how to use them.
Suggested articles: