Is there a way in Javascript to determine whether two strings point to the same location in memory? I know that for objects, ===, ==, and Object.is can be used for this purpose (see MDN: Comparison operators and MDN: Object.is()), but when used on strings, these will compare the contents of the strings, not their references...
Asked
Active
Viewed 475 times
6
-
2There is no way to do this, it's not defined in the official EcmaScript spec. It's an implementation detail of the engine whether or not they want to point multiple references to the same location in memory. – skyline3000 Oct 22 '19 at 20:33
-
That's what's kind of nice about immutable values: You don't really have to care whether a value is a "value-type" or "reference-type" value. It's indistinguishable. – Felix Kling Oct 22 '19 at 20:53
1 Answers
4
JavaScript provides no API for determining any information about memory. It abstracts memory management.
Whether two variables containing identical strings are implemented under the hood as points to the same memory location or not is an implementation detail that you can't control or determine.
Quentin
- 857,932
- 118
- 1,152
- 1,264
-
OK thank you, and in that case I have a [related question](https://stackoverflow.com/questions/58512864/the-v8-javascript-engine-and-string). – Nicolas Trahan Oct 22 '19 at 22:08