I read on MSDN that to improve scripting efficiency, you can use self to make implicit window references explicit.
-
Do you know if this is true? Does this basically mean that for instance calling
self.locationis somewhay more efficient than calling simplylocationwith nowindowopject before? -
Since the MSDN text is referred to the
selfand notwindow, does this performance increase happens only usingself?
According to herewindowandselfandwindow.selfare the same thing, so it shouldn’t matter what we use, i’m asking only to make sure. -
Moreover following what stated in MSDN calling
window.selfshould be somehow more performant than callingselfbecause this last one is a property ofwindowso by callingwindow.selfwe use an explicit ref.
Thanks
Although it’s very much a micro-optimization, direct property references are always faster than variable lookups. When you write
location, something like the following is performed:locationdeclared in the current scope, return and exit if found.locationin global scope and return if found, otherwise throw undeclared variable error.A similar case is made against using the
withstatement to create a scope for object properties. The same goes forself, which is also a property ofwindow.selfis a reference towindow, sowindow.locationshould be faster thanwindow.self.location. Also, remember that implementations can be different, so your mileage may vary from browser to browser.As Pointy “pointied” out, most developers don’t have to worry about micro-optimizations like this. The difference is micro-seconds and completely unnoticeable to end users.
Further reading: