codeepic
codeepic

Reputation: 4112

Difference in memory usage when using objects vs primitive values as function arguments

Whenever I only need one specific object property, I will pass that property to the function, but I've seen my colleagues passing the whole objects only to use 1 value. Intuitively I believe my way is better, but I'd like to really know if it's better purely from the point of memory management.

const largeObj = {...};

dummyFn1(largeObj);
dummyFn2(largeObj.name);

So in essence - is there any difference in memory allocation between these two functions?

Upvotes: 2

Views: 89

Answers (1)

Máté Safranka
Máté Safranka

Reputation: 4106

Objects are passed by reference in JavaScript, so passing an object as an argument is basically just passing a memory pointer, which is negligible.

Upvotes: 3

Related Questions