Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any method to check memory address of Javascript variable?

Tags:

javascript

Is it possible to find the memory address of a JavaScript variable in modern Javascript? Or address of Typescript variable.

I am confused about the pass by value and pass by reference, using memory address it is easy to understand the reality. Now we have now ES8 and Typescript but still unable to get memory address of variable and Object.

like image 536
SAURABH Avatar asked May 25 '18 14:05

SAURABH


People also ask

Are JavaScript variables stored in RAM?

They are stored in RAM. Since its an Implementation-Specific thing, you cannot control it.

How are JavaScript variables stored in memory?

“Variables in JavaScript (and most other programming languages) are stored in two places: stack and heap. A stack is usually a continuous region of memory allocating local context for each executing function. Heap is a much larger region storing everything allocated dynamically.

How much memory does a variable take in JavaScript?

Each boolean and number variable takes 8 bytes of memory.


2 Answers

From this post:

It's more or less impossible - Javascript's evaluation strategy is to always use call by value, but in the case of Objects (including arrays) the value passed is a reference to the Object, which is not copied or cloned. If you reassign the Object itself in the function, the original won't be changed, but if you reassign one of the Object's properties, that will affect the original Object.

like image 176
R.F. Nelson Avatar answered Sep 30 '22 10:09

R.F. Nelson


All JavaScript runtimes that I know of hide this from you.

However, you can simulate the behavior of a memory pointer by using Typed Arrays and an appropriate buffer view based on the type of data you wish to store. You can simulate a pointer by saving your data at a particular offset, and then passing that offset around to different functions that manipulate the data directly in the typed array: aka the offset acts sort of like a low-level pointer.

like image 20
ouni Avatar answered Sep 30 '22 10:09

ouni