News for hackers. Stuff that matters.
Re:Optimizing dynamic JavaScript with inline caches by z3t4 on Friday April 07, 2017 @05:21PM (#14063601)
When it comes to optimization, less if often better. If you have branching it's likely that the CPU will run all branches anyway. You also want to stay in the CPU and off the memory. And bitwise operations are usually not faster in JavaScript. I probably missed the point but blame it on the stupid examples. I also think it's stupid to write dot net C# when you can write JavaScript with NodeJS :P
Re:Optimizing dynamic JavaScript with inline caches by tjallingt on Friday April 07, 2017 @03:52PM (#14063002)
``` var dividers = { 2: function divideBy2 (lhs, unused) { return lhs >> 1 }, 4: function divideBy4 (lhs, unused) { return lhs >> 2 }, undefined: function divideByNumber (lhs, rhs) { return lhs / rhs } } ```

Does this actually work? I tried it out in the Chrome Debugger and it didn't work. It looks like the author is confusing it with Rusts match statement.

Re:Optimizing dynamic JavaScript with inline caches by idbehold on Friday April 07, 2017 @03:58PM (#14063048)
It works just fine in Chrome's DevTools.

  > dividers[2](6)
  3
  > dividers[undefined](6, 3)
  2
Re:Optimizing dynamic JavaScript with inline caches by rndgermandude on Friday April 07, 2017 @04:10PM (#14063121)
The code uses dividers[divisor] so dividers[3](6, 3) so undefined(6,3) and that does not work
Re:Optimizing dynamic JavaScript with inline caches by idbehold on Friday April 07, 2017 @04:17PM (#14063170)
Oh, yeah the divideSomeNumbers() function doesn't work correctly. It should probably be using:

  var divider = dividers[divisor] || dividers[undefined];
Re:Optimizing dynamic JavaScript with inline caches by Lerc on Friday April 07, 2017 @07:47PM (#14064334)
I have been experimenting along these lines. Not so much aiming towards what is optimised but rather what is potentially optimisable.

for instance

    function callerOfAll(fns) {
      if (fns.length===0) return ()=>{};  
      if (fns.length === 1) return fns[0];
      var first = fns.shift();
      var rest = callerOfAll(fns);  
      return ()=>{first();rest();}
    }

This should take a list of functions and return a function that calls them all. An optimiser should be able to inline the function call if it notices each rest() is only ever called once. Ultimately it could turn

    [a,b,c,d,e] into ()=>(a();b();c();d();e());  
or even further if those functions themselves are small enough to be inlinable.

Looking at V8 IR, it doesn't seem to get there.

I have wondered about making a benchmark suite out of such tricky but logically optimisable structures to give Javascript engines something to aim for.