Skip to content

Instantly share code, notes, and snippets.

@tkihira
Last active February 18, 2018 12:47
Show Gist options
  • Save tkihira/aebed3a3ee34cc0ca6e8aecba0c073a8 to your computer and use it in GitHub Desktop.
Save tkihira/aebed3a3ee34cc0ca6e8aecba0c073a8 to your computer and use it in GitHub Desktop.
<html><head><title>WebAssembly: JavaScript binding profiling</title>
<script>
onload = function() {
var textarea = document.getElementById("textarea");
var log = function(message) {
var now = performance.now();
textarea.value += (now - time).toFixed(3) + ": " + message + "\n";
time = performance.now();
};
var time = performance.now();
var c = 0;
var importObj = {
test: {
jsf: function() {
c++;
}
}
};
var profiling = function(filename) {;
log(filename + ": download start");
fetch(filename).then(function(response) {
log("download end");
return response.arrayBuffer()
}).then(function(bytes) {
log("compile start");
return WebAssembly.compile(bytes);
}).then(function(module) {
log("compile end / instantiate start");
return WebAssembly.instantiate(module, importObj);
}).then(function(result) {
log("instantiate end / fib calc start");
var test = result.exports.fib;
log("result:" + test(40));
if(c) {
log("called JavaScript function " + c + " times");
} else {
profiling("fib_binding.wasm");
}
log("--------------------------");
});
};
profiling("fib.wasm");
}
</script>
</head><body><textarea id="textarea" cols="80" rows="20"></textarea></body></html>
asm`fib
 AH@A Ak Akj
(module
(export "fib" (func $fib))
(func $fib (param $n i32) (result i32)
(if
(i32.lt_s
(get_local $n)
(i32.const 2)
)
(return
(i32.const 1)
)
)
(return
(i32.add
(call $fib
(i32.sub
(get_local $n)
(i32.const 2)
)
)
(call $fib
(i32.sub
(get_local $n)
(i32.const 1)
)
)
)
)
)
)
asm `` testjsffib
! AH@A Ak Akj
(module
(export "fib" (func $fib))
(import "test" "jsf" (func $test))
(func $fib (param $n i32) (result i32)
(call $test)
(if
(i32.lt_s
(get_local $n)
(i32.const 2)
)
(return
(i32.const 1)
)
)
(return
(i32.add
(call $fib
(i32.sub
(get_local $n)
(i32.const 2)
)
)
(call $fib
(i32.sub
(get_local $n)
(i32.const 1)
)
)
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment