我试图了解 ASM 究竟是如何工作的以及它何时启动。
我从 asm.js 网站上获取了一个小功能。我使用模块模式包装它:一次用于 asm,一次使用相同的语法但没有“use asm”注释,一次像 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);
};
}())
我创建了一个小 jsperf: jsperf 代码与上面的代码略有不同,结合了下面讨论线程中的提示 http://jsperf.com/asm-simple/7
性能显示 firefox 22 在 asm-syntax 下最慢(有或没有“use asm”注解),而 chrome 在 asm-mode 下最快。
所以我的问题是:这怎么可能?我希望 Firefox 在 asm 模式下最快。我不希望看到 Chrome 有什么不同。我使用了错误的 asm 语法吗?我错过了什么?
非常感谢任何建议或澄清。谢谢,