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,
When you run code in Firefox, you can often see huge drop in speed for asm.js calls, which is most probably caused either by repeated compilation (which is visible in console) or by cost of js-to-asm calls. This hypothesis is further strenghtened by Luke Wagner, implementor of asm.js:
one performance fault that we already know trips up people trying to benchmark asm.js is that calling from non-asm.js into asm.js and vice versa is much slower than normal calls due to general-purpose enter/exit routines. We plan to fix this in the next few months but, in the meantime, for benchmarking purposes, try to keep the whole computation happening inside a single asm.js module, not calling in and out.
To see it for yourself - look at the fiddle: http://jsperf.com/asm-simple/10
- Firefox 26: 22,600K ops/sec in asm-asm case vs 300(!) in asm-js case.
- Chrome 28: 18K vs 13K
- IE11: ~7.5K for all tests, no big difference observed, except for dead code ellimination, where it shines ;)
来源:https://stackoverflow.com/questions/17951449/firefox-does-not-seem-to-be-faster-using-the-asm-js-profile-yet-chrome-is