• Wow, thanks! it's great to see the benchmarks for this. If I'm honest it's a surprise that cks is better than cks4. It's been a real struggle to find anything that's faster, but:

    // just removing whitespace from the cks callback gives you a tiny bit
    function cks9(m) {
      var s = 0;
      new Uint8Array(E.toArrayBuffer(m)).forEach(f­unction (c) {s^=c});
      return s;
    }
    // and pre-binding the function and using a for in loop helps a surprising amount
    function cks10(m) { 
      var s = 0;
      var c = m.charCodeAt.bind(m);
      for (var i in m)s^=c(i);
      return s;
    }
    
    

    But you should be able to use compiled, for instance try:

    function xor(a,b) { "compiled"; return a^b; }
    function cks5(m) {
      return new Uint8Array(E.toArrayBuffer(m)).reduce(xo­r, 0);
    }
    function cks6(m) { 
      "compiled"; 
      var ck = 0;
      var l = 0|m.length;
      for (var i=0;i<l;i++) ck ^= m.charCodeAt(i);
      return ck;
    }
    function cks7(m) { 
      "compiled"; 
      var ck = 0;
      var l = 0|m.length; // ensure we don't have to check the length each time
      var c = m.charCodeAt.bind(m); // ensure we don't have to look up 'charCodeAt' each time
      for (var i=0;i<l;i++) ck ^= c(i);
      return ck;
    }
    

    I just had a quick play and they seem to be 2x or more faster than cks10 (especially the last one). Hope that helps!

  • @Gordon, thanks for your time.

    I am trying it now and just face some errors as compiler server seems to be unjoinable.
    However, without compiling, cks10 is 16% faster than cks which was my best result.

    It is a permanent surprise to see how efficiency can be improved on such a language as javascript.

    So, let's hope the compiler will be accessible soon.

About

Avatar for Gordon @Gordon started