0

This is probably a simple question, but I would really like to know the answer.

Are the arguments passed into a function calculated once and set as local variables or are they calculated every time they're used inside the function?

For example:

When you write a forLoop, you should set a variable that finds the object using the iteration:

  for(var i = 0; i < objects.length; i++) {
     var obj = objects[i];
     obj.title = "title";
     obj.description = "description";
  }

If you don't set the obj variable then the operation for finding the object will be run more than once:

  for(var i = 0; i < objects.length; i++) {
     objects[i].title = "title";
     objects[i].description = "description";
  }

So far I've learnt that this is bad (although I'm guessing the performance difference in modern browsers is almost unnoticeable).

My question is, if you wrapped the modifying methods in a function and passed objects[i] to the function, would objects[i] be calculated once and set as the local variable obj in the function or would it calculate it every time obj is called?

What is a better practice, code A or code B?

Code A:

   function modify(obj) {
      obj.title = "title";
      obj.description = "description";
   }

   for (var i = 0; i < objects.length; i++) {
      modify(objects[i]);
   }

Code B:

   function modify(obj) {
      obj.title = "title";
      obj.description = "description";
   }

   for (var i = 0; i < objects.length; i++) {
      var obj = objects[i];
      modify(obj);
   }

Update: This question is similar but different to this SO question because it simply questions when the value is calculated rather than which value is passed.

Community
  • 1
  • 1
Dol
  • 944
  • 3
  • 10
  • 25
  • 1
    The relevant portion of the possible duplicate is that objects are effectively passed by reference while other variables are passed by value. Meaning when passing objects, the object outside the function will be what is used. If you want to pass the value in a way that it will not affect the variable outside of the function, you need to create a deep copy. – userbd Jun 13 '15 at 19:33
  • So if it's passed by reference then it doesn't recalculate which object in the array each time you use it. This question is much simpler than the possible duplicate, scimonster answered it pretty well :). Thanks for your help @userbd – Dol Jun 13 '15 at 19:41
  • @userbd: No. Objects are passed by value as well. Of course they are reference values, but that doesn't matter for the call. – Bergi Jun 13 '15 at 19:42
  • @Dol: Imo still a duplicate. The answers to the duplicate question state that "*It's always pass by value*" - which means that no reference to the computation is passed like you suggested. – Bergi Jun 13 '15 at 21:44
  • @Bergi, I agree with your comment that you can derive the same answer from the other question, but I think the question is different enough. Happy to change it as needed. – Dol Jun 13 '15 at 22:04

1 Answers1

1

My question is, if you wrapped the modifying methods in a function and passed objects[i] to the function, would objects[i] be calculated once and set as the local variable obj in the function?

Yes. Once the value is passed, it's passed. It doesn't recompile the argument each time.

Therefore, code A will be "better", but i really doubt it makes much of a difference at all....

Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • With objects, the "value" that is passed is a reference to the object, meaning it will continue to be affected by changes to the object outside of the function. – userbd Jun 13 '15 at 19:34