13

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.

Nillus
  • 1,131
  • 1
  • 14
  • 32
  • 4
    in javascript all complex types are passed by reference, so all modifications you'll be doing inside the function will be with original object => there will be no additional memory consumption for copying – shershen Sep 14 '14 at 10:18

2 Answers2

12

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.

Community
  • 1
  • 1
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Case 1, the function that calls the object is in the cycle, so I meant to ask if the process of recovery some properties of an object passed to a function inside it is slower increasingly with the object size - in this sense it was involved with passing it to a function. – Nillus Sep 15 '14 at 08:55
  • @Nillus: Yes, naturally there is a difference, but as I wrote it is minimal. The property lookup is implemented with something like a hash table, so getting a property is close to an O(1) operation. If there are many properties you might get a hash collision sometimes, so then it would look through two-three properties to find the right one. You won't be able to measure if there actually is a difference, because how the larger objects will affect the memory cache will have a much greater impact on the performance. – Guffa Sep 15 '14 at 10:42
  • Can you link "a proof" of what you are saying? Where does Ecma International or Mozila Developer Network or V8 source code or some other respected authority say that? Actually right now your explanation does not help to clarify "..parameter is not passed by reference..confusion about the terms.." and does not look better then the one by @lexicore – xmojmr Sep 15 '14 at 13:13
  • @xmojmr: Why do you think that the explanation doesn't help to clarify the confusion about the terms? The answer is very exact and explains precisely how it works. – Guffa Sep 15 '14 at 17:09
  • @xmojmr: If you want an answer to look in a specific way, then you are free to write your own answer. – Guffa Sep 16 '14 at 08:19
  • For completeness, I read a good post, linked into a simiar question hgere on stackoverflow. whatsthepointy.blogspot.it/2013/11/javascript-does-not-have-pass-by.html. Here it is clearly explained what in practice is a pass-by-reference supporting language, and why Javascript is not one of them. Yet, remains unclear the internal way Javascript uses to pass by copy reference, if it copies the entire object - it is said to copy the reference, but if Javascript does not support pointers, how can this happen without copy the entire object? I think it is a masked implicit pass by pointer... – Nillus Sep 18 '14 at 07:58
  • The other post: http://stackoverflow.com/questions/7744611/pass-variables-by-reference-in-javascript – Nillus Sep 18 '14 at 08:11
  • 1
    @Nillus: Eventhough Javascript doesn't have pointers, an object variable contains what resembles a pointer to the object, regardless of how that is implemented internally. It's very similar in function to a reference in managed languages like C#, where it's implemented using a pointer but the language doesn't let you manipulate the pointer. Passing a reference as parameter in Javascript copies the reference just like when you assign a variable. Using `var a = {}; var b = a;` doesn't create a copy of the object to place in the variable `b`, you just get two references to the same object. – Guffa Sep 18 '14 at 10:26
6

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.

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • 1
    You are mixing up the concepts. The parameter is not passed by reference. The parameter can be a reference, but it's always passed by value. – Guffa Sep 14 '14 at 10:27
  • 1
    I'm not mixing, I'm simplifying. Please note that I've written "by reference" in quotes in a sentence starting with "in simple terms". Strictly speaking, you're right, but I'm not sure this strictness is absolutely necessary for the question being asked. – lexicore Sep 14 '14 at 11:27
  • 1
    The question was primarilly asked just because of all the confusion about the terms. – Guffa Sep 14 '14 at 11:35
  • From this perspective, you're definitely right. I was primarily focusing on the performance aspect. – lexicore Sep 14 '14 at 16:14
  • The performance depends on the functions, it is clear. I am asking, given a function that makes the very same operations, accessing its argument the same number of times and writing it the same number of times, the execution time is the same in the case of large and small obejcts, or there is some overhead for larger? I think I can assume that approximately there is no substancial difference. Am I right? – Nillus Sep 15 '14 at 04:48