简体   繁体   中英

Javascript object function parameters performance

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.

The short answer is that the objects are not copied, just a reference to the objects are passed as the parameter.

The more precise answer is that in Javascript all parameters are passed by value. For simple types like numbers that means that the value is copied. For objects is means that the reference to the object is copied.

As you have noted, the parameter itself is an independent copy, but the parameter points to the same object as the variable that you used in the call to the function.

Edit:

For use case 1 the only difference comes from accessing a property from an object that has more properties. The difference in locating a property among few or many is minimal, the only practical difference you will see comes from the fact that the objects has to be brought into the memory cache as you loop through them, but that has nothing to do with passing them to a function.

For use case 2 there is no duplication of the object, the object still exists only once in memory.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM