I read some threads about how the javascript function parameters passing works when the parameter is an object; I noticed that there is much confusion on the passing method, at least in the terminology: pass-by-reference, pass-by-copy-reference, and so on. This question is not about how is named this passing method, or how it works inside, but involves some way an answer to this question.
I have some large, very large object, to pass to a function as argument; I need to understand if the object-passing implies some copy of the object, so memory consumption, computational effort, memory leak risks are proportional to the size of the object passed, for each function call (I have many calls), or if it is passed in a non size-proportional consequences way.
Since altering the object's properties in the function alters the object in the outer scope, but altering the object itself does not, I think the memory used internally in the function to store and "reference" the parameter is not dependent on its size, because the object seems not to be copied, but I need to be sure about it.
Sorry for the poor explaining of my english!
EDIT: the answer involves in some way an insight on JS passing mode, but the core problem is performance improvement of practical case, so any theorical information is of good use, but the most important information needed is about computational and memory consumption.
Use case 1 (performance): Let's say I have a function that accesses two members of its argument, and writes some result on a third one, executed 1000 times on 1000 different object. The question is: the hypotetical cycle will take almost the same time if the object is made of the only 3 properties involved and if it has other hundred properties? Any difference would be caused only by parameter copy overhead or by selecting properties inside a larger object? Actual test could largely depend on browser, so I need technical, generic-valid answer to this.
Use case 2: I have 100MB object, passed to a function. During the execution time, do I have a memory occupation increase of 100MB? So any memory leak introduced, for example, by miscontrolled enclosures, are more dangerous.
When you call a function in JavaScript, you can pass in any number of arguments, regardless of what the function declaration specifies. There is no function parameter limit. In the above function, if we pass any number of arguments, the result is always the same because it will take the first two parameters only.
A JavaScript function can have any number of parameters. The 3 functions above were called with the same number of arguments as the number of parameters. But you can call a function with fewer arguments than the number of parameters.
It does not matter in which order named actual parameters appear, as long as they are labeled correctly. Simulating named parameters in JavaScript looks as follows.
Please see this answer:
Pass Variables by Reference in Javascript
In simple terms, your object is passed "by reference" and from the performance point of view there should be no difference in calling the function with a huge or a small object.
Having said that, the overall performance depends on the function. It may copy the object, do AJAX calls etc., all of that may or may not perform differenty depending on the size of the object.
For the performance of the function invocation, taken exclusively, there should be no difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With