Skip to content

Instantly share code, notes, and snippets.

@crongro
Created April 15, 2014 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crongro/10708235 to your computer and use it in GitHub Desktop.
Save crongro/10708235 to your computer and use it in GitHub Desktop.
ASM Module performance test
//http://ejohn.org/blog/asmjs-javascript-compile-target/
function DiagModule(stdlib, foreign, heap) {
//"use asm";
// Variable Declarations
var sqrt = stdlib.Math.sqrt;
// Function Declarations
function square(x) {
x = +x;
return +(x*x);
}
function diag(x, y) {
x = +x;
y = +y;
return +sqrt(square(x) + square(y));
}
return { diag: diag };
}
function DiagModuleWithASM(stdlib, foreign, heap) {
"use asm";
// Variable Declarations
var sqrt = stdlib.Math.sqrt;
// Function Declarations
function square(x) {
x = +x;
return +(x*x);
}
function diag(x, y) {
x = +x;
y = +y;
return +sqrt(square(x) + square(y));
}
return { diag: diag };
}
var start1 = new Date();
for(var i = 0 ; i < 1000000; i++) {
DiagModule(window).diag(3,4);
}
var end1 = new Date();
var start2 = new Date();
for(var i = 0 ; i < 1000000; i++) {
DiagModuleWithASM(window).diag(3,4);
}
var end2 = new Date();
console.log("no ASM->" , end1.getTime() - start1.getTime());
console.log("ASM ->" , end2.getTime() - start2.getTime());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment