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...
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.
Memory spaces in V8 “Variables in JavaScript (and most other programming languages) are stored in two places: stack and heap.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With