3

I would like to know (confirm, hopefully) whether JavaScript manages its variable in a copy-on-write manner. This is important because I may end up dealing with possibly large strings, quite a few of them.

var a, b;

a = $(".foo").html();
b = a;

Is b a deep copy or a copy-on-write? My code would benefit very much from a copy-on-write because in some cases I set b to a different value (i.e. in most cases I copy a, in other cases I set to, for example, "on" or "off". However, it doesn't get modified later.)

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • 1
    Have you tried testing it in the console to see what it does? – Andy Mar 18 '14 at 18:08
  • This seems related to: http://stackoverflow.com/questions/51185/are-javascript-strings-immutable-do-i-need-a-string-builder-in-javascript – mrmcgreg Mar 18 '14 at 18:10
  • The short answer is that `b` is a reference to the same string that `a` is a reference to. If you wanted `b` to point to a string with a different value you'd need to create a new string and then make `b` refer to it. – mrmcgreg Mar 18 '14 at 18:12
  • 3
    Both refer to the same memory. There is no deep-copy nor copy-on-write. The `=` assigns references in this example. – Wiktor Zychla Mar 18 '14 at 18:12
  • @testuser, yes! That link is exactly what my question is about and the selected answer is exactly what I expected albeit wasn't sure. – Alexis Wilke Mar 18 '14 at 20:00
  • @Andy, what I'm asking is not something you can easily test. I know `a === b` after the assignment, but that was not the question. – Alexis Wilke Mar 18 '14 at 20:00

2 Answers2

5

Do JavaScript strings use copy-on-write? No, because you cannot write to a JavaScript string, they are immutable.

But, yes, they are effectively using that optimization. When you assign b=a in your example, b is getting a pointer to the same storage that a is pointing to. I.e. it is very quick. If you then do b = b.replace('x','y'), a new string is created, and b points to it, while a continues to point to the original string.

See section 11.2.2 in JavaScript The Definitive Guide, on strings.

BTW, if you are really interested, here are the V8 sources:

https://github.com/v8/v8/blob/master/src/objects/string.h

https://github.com/v8/v8/blob/master/src/objects/string.cc

And a bit of explanation as to why it is so complex (i.e. large strings are sometimes stored as a bunch of small strings, which are only reassembled when necessary; there also seems to be an optimization for ascii strings) https://gist.github.com/mraleph/3397008

Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • P.S. I went to give a link to section 11.2.2, and realized I was looking at a pirate copy, and I'm not sure there is an authorized free source. But google will find lots of pirate uploads for you. – Darren Cook Oct 09 '19 at 08:21
  • V8 documentation does not describe JavaScript, it's a description of a runtime. – Armen Michaeli Oct 09 '19 at 08:21
  • @amn I included it as an example of real-world implementation. (I was also going to look up the Firefox implementation, but didn't have time: I imagine it is very similar.) – Darren Cook Oct 09 '19 at 10:29
0

From everything i know about JavaScript, a === b, but they are not linked. If you change one, the other will not change.

Strings are native types, and hence are referred to (and copied) by value, not reference.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • You did not read the question properly. OP asked about Strings, which are immutable. – Kijewski Mar 18 '14 at 18:49
  • @Kay how do you think i misinterpreted the question? It's true that if it was an ordinary Object, changing properties of one should change it on the other. But strings, numbers, and Booleans don't work like that. – Scimonster Mar 18 '14 at 18:53
  • "If you change one, the other will not change", that's simply what you cannot do. – Kijewski Mar 18 '14 at 18:57
  • Kay is right, the first paragraph was not my question. But you say in the second paragraph strings would be copied instead of copy-on-write referenced. Wouldn't it be a great optimization to have a copy-on-write within the interpreter? And I would think that would be easy to implement too... – Alexis Wilke Mar 18 '14 at 19:57
  • Note that I accepted that answer: http://stackoverflow.com/questions/51185/are-javascript-strings-immutable-do-i-need-a-string-builder-in-javascript#51193 -- they clearly say that they have immutable strings and so not exactly copy-on-write, but very similar. – Alexis Wilke Mar 18 '14 at 20:02