12

Is there a way to see where the variable is located in the memory?

In ActionScript for example in debug mode you can see the memory location of the variable

I am using Google Chrome developer tools where I can see the variables in debug mode but there is no info about the memory location of the variable.

Are there any browser tools that show variable memory location?

onetwo12
  • 2,359
  • 5
  • 23
  • 34
  • 1
    I'm curious, why would you need -- or even *want* -- to know this? –  Oct 12 '13 at 12:12
  • 1
    Can be very useful in debugging some times. If there are two variables with same name you can differentiate them by the memory location. If you debug using 'Step into next function call' sometimes you can easily follow the variable if you know it's memory location. – onetwo12 Oct 12 '13 at 12:18
  • @onetwo12: There cannot be two variables with the same name. If they're in different scopes, then either one of them is inactive or the one shadows the other. The scope chain does show them to you. – Bergi Oct 12 '13 at 13:48
  • 1
    @Bergi: In JavaScript you can see which 'this' variable is in the function scope by knowing the memory location of the 'this' variable. – onetwo12 Oct 12 '13 at 14:49

2 Answers2

11

Take a memory snapshot. This will show in detail what kind of objects are floating around, where and how many they are.

Example view
(source: google.com)

Of course, it won't show you the exact memory or register addresses, but you hardly will need those for debugging javascript.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I think this image needs to be updated with a new one... – EugenSunic Oct 13 '20 at 12:05
  • @EugenSunic Why, the answer is just 7 years old :-) Though the screenshots from the current article (link updated) don't seem to be very recent either. – Bergi Oct 13 '20 at 12:58
7

No, it isn't possible. Due to way javascript interpretes works, it is quite hard to get memory address.

Karol Sikora
  • 522
  • 1
  • 4
  • 11
  • Could you elaborate on the reasons? Values may be in registers, but that's a problem for any debugger and they solve it quite well. –  Oct 12 '13 at 12:04
  • Not only in registers. For example V8 internally represents some variables as objects that merges values from many variables. Its not simple to compute which variable is actually assigned to specific object and how much memory is occupated by single js variable. Thas the reason why tools for getting direct variables adress arent exists. – Karol Sikora Oct 12 '13 at 13:19
  • But it can show the current values, so it clearly *does* figure out where each variable is located. Even if that logic is not sound and all else fails, you can still turn off the problematic optimizations when running w/ debugger on. –  Oct 12 '13 at 13:22
  • Are you actually interested if two variables `a` and `b` are the same object? You could use `===` to see if they are equal. So the JSVM definitely have an `id` concept but it is not exposed. Beware that DOM object also has `id` field that maps to the `id` attribute. See [here](http://stackoverflow.com/questions/1997661/unique-object-identifier-in-javascript) for some example to implement your own identifier. – leesei Oct 12 '13 at 13:36
  • @leesei what if they're in different environments (for instance, in a testing script versus the actual script the test is testing?) – dclowd9901 Apr 08 '16 at 18:44