views:

39

answers:

2

I read on MSDN that to improve scripting efficiency, you can use self to make implicit window references explicit.

  1. Do you know if this is true? Does this basically mean that for instance calling self.location is somewhay more efficient than calling simply location with no window opject before?

  2. Since the MSDN text is referred to the self and not window, does this performance increase happens only using self? According to here window and self and window.self are the same thing, so it shouldn't matter what we use, i'm asking only to make sure.

  3. Moreover following what stated in MSDN calling window.self should be somehow more performant than calling self because this last one is a property of window so by calling window.self we use an explicit ref.

Thanks

+2  A: 

This is the kind of micro-optimization that's really a complete waste of time, but for what it's worth a common idiom is to write Javascript like this:

(function(window, undefined) {
  // your code, thousands of lines of sheer beauty
})(this);

That gives you a local reference to "window", and a reliable "undefined" variable to boot.

Why is it a waste of time? Because for any ordinary code, you're talking about at most a millisecond or two shaved off the execution time. Nobody's ever going to notice that. Make sure that the actual algorithms you're using to do whatever it is you're coding are appropriate, and let the Javascript interpreter/JIT developers shave off those milliseconds for you. If you get obsessed with things like this, you're liable to end up with code that runs slower in the future because you'll have done weird things that end up not being optimized by the interpreter.

Pointy
@Pointy: I know the performance increase (if there is one) is extremly small, but mine it's just pure curiosity. Your code seems interesting, could you explain something more? I dodn't understand why I need it, window is in global scope why do I need a local reference to it? And what's 'undefined' parameter there for? Thanks.
Marco Demajo
You don't *need* a local reference, but "window" is just a property name and it can be reassigned. By passing "this" from the global context, you get a better guarantee that your local "window" will reference the right thing. Plus, since it's a local variable, it'll be a tiny bit faster than referencing the global "window". The "undefined" thing is similar: "undefined" is just a property and can be changed. By *not* passing a parameter, you get a **real** undefined variable to use in your code. Again, this is just a modern convention (see jQuery).
Pointy
You can also set *undefined* using `var undefined = void 0`, which is useful in situations where other developers might be calling your function which uses an unset argument for *undefined*. If said developer accidentally set the argument, it could cause issues.
Andy E
This optimization also allows javascript minifiers to rename the window parameter to a shorter name, which isn't possible when window refers to the global object
Kevin Hakanson
+1  A: 

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:

  1. Look for location declared in the current scope, return and exit if found.
  2. Go up the scope hierarchy by one.
  3. If scope is not Global, go to 1. If scope is Global, check for location in global scope and return if found, otherwise throw undeclared variable error.

A similar case is made against using the with statement to create a scope for object properties. The same goes for self, which is also a property of window. self is a reference to window, so window.location should be faster than window.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:

Andy E