-
-
Save dom96/b9d613396f3cdedd9e45438ce23f7d04 to your computer and use it in GitHub Desktop.
Fib Wasm Bench
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> | |
<title>Webasmio - Fib benchmark</title> | |
<!-- Depend on WABT for now to convert WAST to WASM. --> | |
<script src="https://unpkg.com/wabt@v1.0.15/index.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.4/benchmark.min.js"></script> | |
<script src="./fib.js"></script> | |
<!-- <script src="./fib_js.js"></script> --> | |
<script> | |
function fib_js_native(n) { | |
var a = 0 | |
var b = 1 | |
var i = 0 | |
while (i < n) { | |
let t = a + b; | |
a = b; | |
b = t; | |
i = i + 1; | |
} | |
return b; | |
} | |
var suite = new Benchmark.Suite; | |
console.log(fib_webasm(40)); | |
// add tests | |
suite.add('webasm', function() { | |
fib_webasm(40); | |
}) | |
// .add('js', function() { | |
// fib_js(40); | |
// }) | |
.add('js_native', function() { | |
fib_js_native(40); | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
// run async | |
.run({ 'async': true }); | |
</script> | |
</head> | |
<body style="margin: 0; overflow: hidden;"> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gWebasmioInstance = null; | |
function fib_webasm(n_224072) { | |
return gWebasmioInstance.exports.fib_webasm(n_224072); | |
} | |
// Create a module from a WebAssembly Text format: https://stackoverflow.com/a/60921153/492186 | |
var myModule = WabtModule().parseWat("nim.wat", ` | |
(module | |
(type $t0 (func (param i32) (result i32))) | |
(func $fib_webasm (export "fib_webasm") (type $t0) (param $p0 i32) (result i32) | |
(local $l0 i32) (local $l1 i32) (local $l2 i32) | |
i32.const 0 | |
set_local $l0 | |
block $B0 | |
get_local $p0 | |
i32.const 2 | |
i32.lt_s | |
br_if $B0 | |
get_local $p0 | |
i32.const -1 | |
i32.add | |
set_local $l1 | |
i32.const 1 | |
set_local $p0 | |
i32.const 0 | |
set_local $l2 | |
loop $L1 | |
get_local $l2 | |
get_local $p0 | |
i32.add | |
set_local $l0 | |
get_local $p0 | |
set_local $l2 | |
get_local $l0 | |
set_local $p0 | |
get_local $l1 | |
i32.const -1 | |
i32.add | |
tee_local $l1 | |
br_if $L1 | |
end | |
end | |
get_local $l0) | |
(table $T0 1 1 anyfunc) | |
(memory $memory (export "memory") 17))`, {}); | |
// Emit module in a binary format | |
var wasmData = myModule.toBinary({}).buffer; | |
// Use WebAssembly API to instantiate a compiled module | |
var compiled = new WebAssembly.Module(wasmData); | |
gWebasmioInstance = new WebAssembly.Instance(compiled, {}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment