News for hackers. Stuff that matters.
Re:How to write up to 98% faster JavaScript code. by viraptor on Friday April 15, 2011 @04:50PM (#2452146)
I'm not sure it would be legal to fix up automatically. What is expected if you have a background timer which could change "window.Library.util" at some point? If you resolve every time, you'll get a new "add()", if you cache, you'll get the same.
Re:How to write up to 98% faster JavaScript code. by dstein on Friday April 15, 2011 @05:04PM (#2452206)
You're right maybe it can't be done universally. But you could add an optimizer to a production build system which rewrites any instance of:

  window.Library.util.add()
To:

  var window_Library_util_add = window.Library.util.add;
  window_Library_util_add();
Re:How to write up to 98% faster JavaScript code. by reeses on Friday April 15, 2011 @05:31PM (#2452292)
Are you saying that the V8 JIT won't catch this? What if it optimizes _any_ function/module/etc. that is later changed? The optimization should be discarded when the reference is reassigned. If it's not, then V8 has a bug that should be manifesting everywhere.
Re:How to write up to 98% faster JavaScript code. by fleitz on Friday April 15, 2011 @06:15PM (#2452439)
The problem is that the code is not semantically identical, there is no way for the compiler to know that nothing else has changed while the loop is executing. It's like the frequent optimization case of

  int len = array.length
  for(i = 0; i < len; i++){

  } 
vs.

  for(i = 0; i < array.length; i++){
  
  }
The code is semantically different and therefore has a different performance profile.
Re:How to write up to 98% faster JavaScript code. by viraptor on Friday April 15, 2011 @08:58PM (#2452828)
Depends what you mean by "changed". The function body itself cannot change in javascript afaik. The function bound to some name can change, but that's equivalent to the name pointing at another structure. The new structure will have its own compiled version of the new function. It doesn't mean though that it can just fold multiple resolutions of the same name into a local variable and cache it. This kind of behaviour would be similar to optimising random() to return 4.

I'm not sure if v8 does any whole-module optimisations... if it does, then there's got to be loads of "invalidate me" hooks everywhere. I doubt it would be worth doing, but if anyone has more precise info - please correct me :)