An asm.js TLE DP Solution; Hopes NodeJS may support asm.js someday


  • 0
    T

    This is a TLE Solution. I just want to share these codes to show the performance on nodejs and jsshell. It is interesting...

    It costs about 5.1s on NodeJS, but 1.1s on JSShell.

    var ab = new ArrayBuffer(200000002);
    var data = new Uint8Array(ab);
    data[0] = data[1] = 0;
    
    var integerReplacement = (function (stdlib, foreign, memory) {
      "use asm";
      var min = stdlib.Math.min;
      var Uint8Array = stdlib.Uint8Array;
    
      var cache = new Uint8Array(memory);
      var l = 2;
    
      function integerReplacement(n) {
        n = n | 0;
    
        var i = 0;
        var one = 0;
        var two = 0;
    
        i = l | 0;
    
        if ((n | 0) < (l | 0)) return cache[n | 0] | 0;
        for (l = (n + 1) | 0; (i | 0) < (l | 0); i = (i + 1) | 0) {
          if (((i & 1) | 0) == 1) {
            cache[i | 0] = min(((cache[(i - 1) | 0] | 0) + 1) | 0, ((cache[(i + 1) >>> 1] | 0) + 2) | 0) | 0;
          } else cache[i] = ((cache[i >>> 1] | 0) + 1) | 0;
        }
        return cache[n] | 0;
      }
      return {
        integerReplacement: integerReplacement
      };
    }({
      Math: { min : Math.min },
      Uint8Array: Uint8Array
    }, null, ab)).integerReplacement;
    
    integerReplacement(200000000);
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.