0

Technical context:

Reading this page about how JavaScript pass Objects by sharing to functions.. Now I know an object inside the called function is the same as in the first outsider scope, unless it is directly assigned to.

This is how I could put it in words. But the example code in the page is way better and simpler.

My question is the following:

Because I'm trying to improve my calculation method, I'm having a very basic question. Is passing a smaller derived object (let's say a sub dictionary with only important keys) to a function for reading only (not to be assigned to) is better for performance (running time) ?

For sake of simplicity, here is an example

function func(){
     var bigobj = {a:1, b:2, c:3, d:4}; 
     otherFunc(bigobj.a);  // will we gain some time against calling otherFunc(bigobj);  ??
     function otherFunc(obj){
         //obj is a smaller representation of bigobj
         //obj will not be assigned to here
    }
}
Curcuma_
  • 851
  • 2
  • 12
  • 37
  • Possible duplicate of [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Andreas Apr 17 '18 at 14:57
  • I am well aware how objects against primitives are passed in Javascript. I don't thing this is a duplicate, I'm asking whethere referecing a sub object in the calling instruction is better that referencing the sub object inside the function! – Curcuma_ Apr 17 '18 at 15:13
  • 1
    Objects are passed by reference, a number which points anywhere in the stack/heap. The actual size of the object is completely irrelevant. If `bigobj.a` is a string which contains the magna carta than `otherFunc(bigobj.a)` would be way "heavier" than `otherFunc(bigobj)` because the content of `bigobj.a` has to be copied. If `bigobj.a` is also an object its just another number... But unless you have to write a real-time application in JavaScript those are micro optimisations which should only be looked at after using a specialized profiler. – Andreas Apr 17 '18 at 15:21

1 Answers1

3

No, because objects are references in JavaScript. The only thing that has to be passed to your function in this case is the reference which is independent of the object size.

If you were passing around values (strings, numbers, etc...), you would be copying the value every time you made a function call, which would incur some additional performance cost. However, unless you're working with massive values, this is unlikely to cause a major performance hit.

Max Hudson
  • 9,961
  • 14
  • 57
  • 107
  • ok, but what about the alternative! I mean calling `otherFunc(bigobj.a);` against `otherFunc(bigobj);` ??! – Curcuma_ Apr 17 '18 at 15:11
  • 1
    @Curcuma_ it's hard to be sure without knowing what otherFunc does, but there is no difference in performance cost between referencing a value within `bigobj` inside or outside the function, unless you're doing it many times inside `otherFunc`. All signs indicate you should be focusing on other types of performance pitfalls. You're probably not going to have much trouble unless you're working with async code, html5 canvas, creating massive numbers of objects, working with massive values, or doing any of those things inside massive loops. – Max Hudson Apr 17 '18 at 17:24