Javascript object function parameter parameters

I read some threads about how the parameter passing of the javascript function goes when the parameter is an object; I noticed that there is a lot of confusion in the method of going through, at least in terminology: link-by-link, link-copy, etc. This question is not about what this transfer method is called or how it works internally, but somehow answers this question.

I have a large, very large object to jump to a function as an argument; I need to understand whether the transfer of an object to an object is implied by some copy of the object, so the memory consumption, computational efforts, risks of memory leak are proportional to the size of the transferred object, for each function call (I have many calls), or if it is transmitted with non-standard proportional consequences.

Since changing the properties of an object in a function changes the object in the external scope, but the object itself does not change, I think that the memory used inside the function to store and “reference” this parameter does not depend on its size, because the object seems is not copied, but I have to be sure of that.

Sorry for the poor explanation of my English!

EDIT: the answer is somehow an idea of ​​the JS transfer mode, but the main problem is to increase the productivity of the practical case, so any theoretical information has good use, but the most important information that is needed is the consumption of computer technology and memory.

Use case 1 (performance): let them say that I have a function that accesses two members of its argument and writes some result to the third, executed 1000 times on 1000 different objects. The question is that a hypothetical cycle will take almost the same time if an object is made of only 3 related properties and does it have another 100 properties? Any difference can be caused only by service data with parameters or a choice of properties inside a larger object? The actual test can depend a lot on the browser, so I need a technical, general answer to this question.

Use case 2: I have a 100 MB object passed to a function. At runtime, do I have a 100 MB increase in memory usage? Thus, any memory leak introduced, for example, by uncontrolled shells, is more dangerous.

+7
performance javascript function object
source share
2 answers

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

A more accurate answer is that in Javascript all parameters are passed by value. For simple types, such as numbers, this means that the value is copied. For objects, means that the reference to the object is copied.

As you already noted, the parameter itself is an independent copy, but the parameter points to the same object as the variable that you used when calling the function.

Edit:

In the case of using 1, the only difference is access to the property of an object that has more properties. The difference in positioning among few or many of them is minimal, the only practical difference that you will see is that the objects should be cached when passing through them, but this has nothing to do with their transfer to the function.

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

+6
source share

Please see this answer:

Pass variables by reference in javascript

Simply put, your object is passed “by reference”, and in terms of performance, there should be no difference in calling a function with a huge or small object.

Having said that, overall performance depends on the function. It can copy an object, make AJAX calls, etc. All this may or may not be performed differently depending on the size of the object.

There must be no difference to make a function call taken exclusively.

+2
source share

All Articles