I am trying to understand how exactly ASM works and when it kicks in.
I took a small function from the asm.js website. I wrap it using the module pattern: once for asm, once with the same syntax but without the "use asm" annotation, and once like vanilla-javascript.
var add_asm = (function MyAOTMod(stdlib, foreign, heap) {
"use asm";
var sqrt = stdlib.Math.sqrt;
function square(x) {
x = +x;
return +(x * x);
}
return function(x, y) {
x = +x; // x has type double
y = +y; // y has type double
return +sqrt(square(x) + square(y));
};
}(window));
var add_reg_asmstyle = (function MyAsmLikeRegularMod() {
function square(x) {
x = +x;
return +(x * x);
}
return function(x, y) {
x = +x; // x has type double
y = +y; // y has type double
return +Math.sqrt(square(x) + square(y));
};
}());
var add_reg = (function MyStrictProfile() {
"use strict";
return function(x, y) {
return Math.sqrt(x * x + y * y);
};
}())
I created a small jsperf: the jsperf code is slightly different from the above, incorporating tips from the discussion thread below http://jsperf.com/asm-simple/7
The performance shows that firefox 22 is slowest with the asm-syntax (with or without the "use asm" annotation), and chrome is fastest in asm-mode.
So my question is: how is this possible? I would expect Firefox to be fastest in asm mode. I would not expect to see a difference for Chrome. Do I use a wrong asm syntax? What am I missing?
Any advice or clarification is greatly appreciated. Thanks,