-1

Does passing the entire object as argument rather than just a property of it, in javascript, effect performance? For example:

<input type="button" onclick="getDetails(this)"/>

vs

<input type="button" onclick="getDetails(this.sourceIndex)"/>

even if I pass the sourceIndex property of 'this' object, I would retrieve the entire element from it in my function - getDetails(). I need advise on what exactly will be the effect in this particular case.

  • Possible duplicate of Is micro-optimisation important when coding? – gnat Feb 4 '16 at 14:21
  • 2
    JavaScript is a language. There are many implementations of JavaScript which may have different performance characteristics. Your best course of action is to profile it and see if it is a bottleneck or not in your chosen engine. If it isn't a bottleneck, don't worry about it. – user40980 Feb 4 '16 at 14:31
  • 2
    I'd be more concerned with making it clear whether or not the getDetails method only needs a sourceIndex argument or if it really does need the entire this object to function correctly. – Ixrec Feb 4 '16 at 14:38
  • The getDetails method does not need just the sourceIndex property. It is used in the function to retrieve the element using document.all(sourceIndex) and then multiple properties of this object are used at various places in the method. – Arpita Ckb Feb 4 '16 at 14:42
3

I feel like profiling this would be overkill. Technically you're passing less on the second way but the performance hit/gain (if any at all) using either is negligible. At this point it's more of about maintainability. Normally you would attach the the event via JavaScript as well but if for some reason you can't and you will need the whole element anyway, going with the first one will save you the hassle of retrieving the element unnecessarily.

  • I wish i could, but i can't use that in my problem. I am maintaining a legacy application. It creates a series of checkboxes inside separate divs through loops and assigns the same id for all the checkboxes. Hence the sourceIndex is used to retrieve the particular checkbox which fires the event. Now in ie11 , i am not getting correct sourceIndex for the event. I have to find a workaround but can't really change the structure of the application. – Arpita Ckb Feb 4 '16 at 15:18
  • @ArpitaCkb if that is your problem, they why aren't you asking about that (which would probably be a reasonable SO question given sufficient information)? – user40980 Feb 4 '16 at 15:21
  • @MichaelT because my question is, given these two options.. is the first one going to affect the performance of my application? – Arpita Ckb Feb 4 '16 at 15:45
  • @ArpitaCkb Probably not. You would need to profile it to be sure. If it is going to be an issue (again, I doubt it but can't say for sure), we need more information about it like "what is the object fully defined as" and the rest of the context of the code. For all we know, that property is actually a function call that does other things. This is also an issue of what engine are you using. The language alone can't say for sure. Is this Chakra? Rhino? Spidermonkey? Different engines may have different performance - unlikely in this case, but there isn't enough context. You need to profile it. – user40980 Feb 4 '16 at 15:51
  • @ArpitaCkb: This is almost certainly something you should not be worrying about unless it actually becomes identified as a performance problem through profiling. Which it won't; if there is a performance problem, it's almost certainly occurring somewhere else. – Robert Harvey Feb 4 '16 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.