Search code examples
javascriptasm.js

How to test the availability of asm.js in a web browser?


Imagine I have a asmjs script but before running the script, I'd like to test and see if the browser supports asm.js or not. If it is false, display a message indicating that the browser is old or something like that, otherwise, execute the script.

Can we utilize the idea of "use asm" somehow to detect if a web browser supports asm.js?

function MyAsmModule() {
    "use asm";
    // module body
}

Solution

  • The sad situation is that there appear to be no real reliable way to detect browser support of asm.js.

    Running this segment will produce an error, but not a catchable one:

    try {
      (function MyAsmModule() {"use asm"})();
      console.log("asm.js OK");
      // Now, hit F12 to open the browser console just to find a TypeError that states:
      // "asm.js type error: expecting return statement"
    }
    catch(err) {
      // will never show...
      console.log("asm.js not supported.");
    }

    This is one of those cases you unwillingly have to turn to client string checking, perhaps combined with other feature checking to determine which browser and version you're dealing with.

    When that information is obtained then check this list which following browsers and versions supports (courtesy of caniuse.com) asm.js, as of this date:

    Version number is since and including - I included also browsers with very small user bases as the information was available for these as well:

    Firefox             : v. 22
    Chrome              : v. 28    (*)
    Edge                : v. 13
    Opera               : v. 15    (*)
    Android browser     : v. 56    (*)
    Opera mobile        : v. 37    (*)
    Chrome for Android  : v. 59    (*)
    Firefox for Android : v. 54
    Samsung internet    : v.  5    (*)
    QQ browser          : v.  1.2  (*)
    Baidu browser       : v.  7.12 (*)
    

    (*): "Chrome does not support Ahead-Of-Time compilation but performance doubled in Chrome 28"