lukasyno Posted April 12, 2014 Share Posted April 12, 2014 Hello There! I have two simple class for example ofc.Class1. function Item(){this.test = N;//N = random value;}Item.prototype.f = function(){ var a = this.test * 10;var b = this.test * 20;var c = this.test * 30;}Class2function Item(){this.test = N;//N = random value;}Item.prototype.f = function(){var test = this.test; var a = test * 10;var b = test * 20;var c = test * 30;}Class Item for example is create about x 1000.And question, CLass1 or Class2 is faster?cost create local variable is small ? or better is just use "this" to find instance value...Cheers. Quote Link to comment Share on other sites More sharing options...
Jorasso Posted April 12, 2014 Share Posted April 12, 2014 http://jsperf.com/this-versus-local-variable Quote Link to comment Share on other sites More sharing options...
ericjbasti Posted April 12, 2014 Share Posted April 12, 2014 Nice to see that you really wont notice a difference in terms of speed between the two. Where you'll see a difference is with memory usage. Since this.test is part of the object it will never get Garbage Collected. Making var test = this.test , the 'var test' (and every other var inside the function scope) will eventually get Garbage Collected. As others will probably point out, you need to focus more or minimizing memory recycling than you do speed of execution. It might take 10000 cycles and 10megs of garbage for it to kick in, but when it does the temporary performance drop from the GC will be a total buzzkill. Quote Link to comment Share on other sites More sharing options...
Cless Posted April 22, 2014 Share Posted April 22, 2014 Unless it makes sense, it may be best not to create new properties on "this" outside of the constructor function just because you think it may be faster. i.e. don't use "this.a = 1" just because you think it's faster or something. Never mind the fact that premature optimization isn't a good idea, changing the shape of an object (adding/deleting properties on an existing object) will force deoptimization in most JavaScript VMs, which will slow down execution and increase GC frequency until it is eventually optimized again. This probably won't be an issue unless you routinely tack on new properties to existing objects. The point is that you should do whatever makes sense for the application. Quote Link to comment Share on other sites More sharing options...
away168 Posted April 22, 2014 Share Posted April 22, 2014 In spite of performance which almost has no difference unless you make a function return call (ex.: system.getFoo().bar, more than 1 time) which of course it would, the 2nd one is preferable for me as one of my best practices. My consideration is you don't need to change your local variable name you make reference to, if you want to change this.test. It's worth my development time, easier to read, and less words (less this'es) which means the JS file is much smaller. But then again, if they are only 2 lines, then there's no point to make such reference like class 2. As for it will be garbage collected I don't know, I don't know the underlying VM code like. What I believe is, local variable will just go to stack allocation like C++ does, where if the variables are out of scope then they are automatically popped out. I am probably wrong since all the variables are indeed objects after all. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.