Reputation: 13
I am trying to understand how actually memory allocation works and I have a question.
var a = {name: 'John', age: '20'};
console.log(a.name);
and
var a = {name: 'John', age: '20'};
var name = a.name;
console.log(name);
I know that both of them give the same result, but I want to know whether the memory usage of both of these codes is the same or not.
Upvotes: 0
Views: 76
Reputation: 3377
Each declared variable will occupy a space in your memory, in the first example:
var a = {name: 'John' , age: '20'};
console.log(a.name);
var a is declared and thus a space is reserved for it, and in the second example:
var a = {name: 'John' , age: '20'};
var name = a.name;
console.log(name);
Besides from declaring var a, you have also reserved another space for var name
This link from MDN can provide a better insight:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management
Hope this was useful for you!
Upvotes: 1