Transfer a large existing object or create and transfer a small object

Suppose I have a large object that contains the required properties, as well as several methods and several significant collections.

I would like to know what will cost more: pass as an argument this large object that already exists, or create a small object containing only a few properties that I need, and pass this?

+7
source share
2 answers

If you just pass it as an argument to the method, transferring the "large" object is "cheaper" - you will only pass a copy of the object link, which is equal to the size of the pointer (if the object does not have a structure type, in which case the entire "object" is copied onto the stack ) If you created a new object, even if it was small, you would have paid the price for selecting and copying properties to it, which you do not have if you are transferring a "large" object.

If you pass it in such a way that it needs to be serialized (i.e. through applications, when calling a web service, etc.), then with a smaller object (essentially a DTO, a data transfer object) is preferred.

+14
source

As long as you follow the link, it does not matter. Therefore, you should not introduce additional complexity. In fact, it will cost more than a car, since then he will have to create this container object.

Since you seem to be interested in performance, I would suggest learning how pointers and memory management work. When you understand this, it will be much easier for you to understand how their abstract versions in higher-level languages โ€‹โ€‹affect performance.

+2
source

All Articles