0

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.

Arpita Ckb
  • 21
  • 2
  • Possible duplicate of [Is micro-optimisation important when coding?](http://programmers.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding) – gnat Feb 04 '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](http://stackoverflow.com/q/855126) and see if it is a bottleneck or not in your chosen engine. If it isn't a bottleneck, don't worry about it. –  Feb 04 '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 04 '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 04 '16 at 14:42

1 Answers1

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.

Ryan
  • 31
  • 3
  • 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 04 '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)? –  Feb 04 '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 04 '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. –  Feb 04 '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 04 '16 at 15:51
  • Now I feel like my "answer" was better off as a comment. But I'm reputation broke so I'll take this chance to express my agreement with the two comments above me. If you want to stick with the second route and are having issues with IE 11, that will probably have to be another question. – Ryan Feb 04 '16 at 15:54