Jump to content

Performance, cost create local variables


lukasyno
 Share

Recommended Posts

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;}

Class2

function 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.

 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...