admin管理员组

文章数量:1355077

I have been working on web project for past 4 months. To optimise the code performance we have used a pattern. My doubt is, does it actually boost performance or not?

when ever we have to use this object we assign it to a local variable, and use that.

function someFunction()
{
  var thisObject = this;
  //use thisObject in all following the code. 
}

the assumption here is that, assigning this object to a local stack variable will boost the performance.

I have not seen this type of coding anywhere so doubt if it is of no use.

EDIT: I know that assigning this object to local variable is done for preserving object, but that is not our case.

I have been working on web project for past 4 months. To optimise the code performance we have used a pattern. My doubt is, does it actually boost performance or not?

when ever we have to use this object we assign it to a local variable, and use that.

function someFunction()
{
  var thisObject = this;
  //use thisObject in all following the code. 
}

the assumption here is that, assigning this object to a local stack variable will boost the performance.

I have not seen this type of coding anywhere so doubt if it is of no use.

EDIT: I know that assigning this object to local variable is done for preserving object, but that is not our case.

Share Improve this question edited Apr 17, 2012 at 8:15 Tejesh Alimilli asked Apr 16, 2012 at 14:37 Tejesh AlimilliTejesh Alimilli 1,8001 gold badge21 silver badges31 bronze badges 2
  • 1 It is extremely mon in Node.js stuff. But that is more for holding a reference in callbacks, as opposed to a performance boost I believe. – Chad Commented Apr 16, 2012 at 14:39
  • 2 I'm going to guess that this is actually worse for performance. Google Closure Compiler (with advanced optimizations) will remove the var declaration and replace instances of thisObject with this, even though using a variable would often result in a smaller minified size. Closure piler optimizes for speed and size, but seems to favor speed over size when it has a choice. – Dagg Nabbit Commented Apr 16, 2012 at 14:45
Add a ment  | 

3 Answers 3

Reset to default 8

While this is a mon practice in Javascript it's not done for performance reasons. The saving of the this object in another named local is usually done to preserve the value of this across callbacks which are defined within the function.

function someFunction() {
  var thisObject = this;
  var someCallback = function() {
    console.log(thisObject === this);  // Could print true or false
  };
  return someCallback;
}

Whether or not thisObject === this evaluates to true will depend on how it's called

var o = {} 
o.someFunction = someFunction();
var callback = o.someFunction();
callback();        // prints false
callback.call(o);  // prints true

As with ALL performance questions, they should be examined by actually measuring the performance. In a rather simple test case (your actual code might vary some), I find mixed results across the browsers:

Chrome and Firefox don't differ very much between the two tests, but the slight difference is in opposite directions between the two. IE9 shows the test using a saved copy of this which I called self to be significantly slower.

Without a significant and consistent performance difference in Chrome and Firefox and IE9 showing the this test case to be substantially faster, I think you can conclude that the design pattern you asked about is not delivering a consistent performance boost across browsers.

In my code, I save a copy of this to another variable only when I need it for a consistent reference to the original object inside of inline event handlers, callbacks or methods where this has been set to something else. In other words, I only apply this design pattern when needed.

In a previous discussion of this design pattern here on SO, it was concluded that some libraries use this design pattern in order to allow additional minimization because this cannot be minified below the four characters that it takes up, but assigning it to a locally variable can be minified to a single character variable name.

Even when this kind of optimization has an (positive) effect, it is very likely to depend on the interpreter.

An other release may revert the results.

However, in the end you should measure, not guess.

本文标签: javascript this objectStack Overflow