General question:
Does it affect performance when a large object is passed as a parameter vs when a native variable is passed?
Case:
I've written a component that manages Google Maps.
In each of the methods of the component, it requires passing in the Google Maps object since I don't want to set the map as a property on the component.
General question:
Does it affect performance when a large object is passed as a parameter vs when a native variable is passed?
Case:
I've written a component that manages Google Maps.
In each of the methods of the component, it requires passing in the Google Maps object since I don't want to set the map as a property on the component.
4 Answers
Reset to default 16When you pass an object as an argument to a function - the only thing that is copied is the handler of that object (that's the address in memory where the object is stored). The object itself doesn't get cloned, so there's no overhead when you pass a big object as an argument.
If you pass a string it does get cloned, so in that case the length of the string is a concern.
In JavaScript it is always pass-by-value. But when an object is passed, the value itself is a reference.
Therefore, performance would not be affected by passing the large object, because what is being passed is a value that is just a reference to the object.
The size of object doesn't affect performance as objects in javascript are passed as reference.
There is a slight performance dip, since the object's location is being called when you send it to your method, but the ease of programming a new component or finding a bug in the object far outweighs the ~1 millisecond cost.