Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get object memory address and object hash in JavaScript [duplicate]

Tags:

javascript

As the title of the question states, I was wondering if there is way to print the object memory address and if there is internal object hash? I know that I can use the object comparator:

var x = {id:1};
var y = x;
console.log(x === y); // => true

but I want to get a string/number representation of memory address and string representation of internal hash if there is one...

like image 931
anchor Avatar asked Sep 19 '18 11:09

anchor


People also ask

Do JavaScript objects use hashing?

A JavaScript Object is an example of a Hash Table because data is represented a key/value pairs. A hashing function can be used to map the key to an index by taking an input of any size and returning a hash code identifier of a fixed size.

How is JavaScript object stored in memory?

Memory spaces in V8 “Variables in JavaScript (and most other programming languages) are stored in two places: stack and heap.

How do I copy values from one object to another in JavaScript?

Using Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object.

How is an object property referenced?

Objects are assigned and copied by reference. In other words, a variable stores not the “object value”, but a “reference” (address in memory) for the value. So copying such a variable or passing it as a function argument copies that reference, not the object itself.


1 Answers

Neither of these pieces of information is available in JavaScript. If an environment were to provide it, it would be part of that environment's feature set, not JavaScript itself.

Also beware that the memory address of an object is not necessarily a constant thing across the lifetime of the object. In particular, modern JavaScript engines may allocate objects created within a function on the stack, and then copy them from the stack to the heap if the object is going to survive termination of the function. (They may well also move them around if necessary when doing garbage collection, though I don't specifically know that they do.) So even if you could get the address, it could well become invalid moments later. But if an environment made the information available, presumably it would come with environment-specific caveats in that regard.

like image 163
T.J. Crowder Avatar answered Sep 23 '22 11:09

T.J. Crowder