Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass around large object as argument

Tags:

javascript

General question:
Does it affect performance when a large object is passed as a parameter vs when a native variable is passed?

Case: I've written a component that manages Google Maps.
In each of the methods of the component, it requires passing in the Google Maps object since I don't want to set the map as a property on the component.

like image 459
html_programmer Avatar asked Jul 07 '15 17:07

html_programmer


People also ask

Can you pass an object as an argument?

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.

When an object is passed by value as an argument?

Pass by value means that a copy of the actual parameter's value is made in memory, i.e. the caller and callee have two independent variables with the same value. If the callee modifies the parameter value, the effect is not visible to the caller. Overview: Passes an argument by value.

What do you mean by passing argument?

Unless an argument is passed by value (BYVALUE), a reference to an argument, not its value, is generally passed to a subroutine or function. This is known as passing arguments by reference, or BYADDR.

What is passed to a function when an object is passed as an argument to a function by value?

Objects as Function Arguments in c++ When an object is passed by value, a copy of the actual object is created inside the function. This copy is destroyed when the function terminates. Moreover, any changes made to the copy of the object inside the function are not reflected in the actual object.


1 Answers

When you pass an object as an argument to a function - the only thing that is copied is the handler of that object (that's the address in memory where the object is stored). The object itself doesn't get cloned, so there's no overhead when you pass a big object as an argument.

If you pass a string it does get cloned, so in that case the length of the string is a concern.

like image 198
asiop Avatar answered Oct 14 '22 20:10

asiop