4

Let's say I've got an object:

var o1 = {
    someKey: 'value'
};

and another object that references that first object:

var o2 = {
    o1Ref: o1
};

and also a third object that references a property on the first object:

var o3 = {
    o1propRef: o1.someKey
};

Then, let's say o2 is garbage collected. Does the reference to o1.someKey on o3 prevent o1 from being collected?

Then, also, suppose o1 is bigger, say:

var o1 = {
    someKey: 'value',
    someBigValue: Buffer(2000000)
};

Can the parts of o1 that aren't being referenced be collected, or are objects collected as a whole? It seems like, with the second version of o1, o3 is just holding on to o1.someKey and o1.someBigValue can be freed up.

Also, I do realize this might vary among implementations. If that's the case, whats' the best way to think about this generally?

Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160

1 Answers1

3

Does the reference to o1.someKey on o3 prevent o1 from being collected?

No, because there isn't one (a reference). You have a copy of the value of o1.someKey as of when you created the object, not a reference to the o1.someKey property. (JavaScript doesn't have references to anything but objects; so no property references, just object references.)

You can see that you're just getting the value as of the initialization by playing with it:

var o1 = {
    someKey: 'value'
};

var o3 = {
    o1propRef: o1.someKey // (It's not a property reference, it's a copy of the value, but I left the name alone)
};

console.log(o3.o1propRef);  // "value"
o1.someKey = "updated value";
console.log(o3.o1propRef);  // "value"
console.log(o1.someKey);    // "updated vale"

For o3 to prevent o1 from being garbage-collected, o3 would have to have a reference to o1 (or to something that in turn has a reference to it). Just getting a value from o1.someKey doesn't set up any kind of reference relationship at all.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thank you T.J.—that's what I was suspecting. I kept wanting to delete my uses of the word "reference" since I do know JS passes things around by value, but I then I thought, perhaps outdatedly, that JS implementations employ "reference counting"? – Dmitry Minkovsky Feb 18 '14 at 17:59
  • Also, just went to your profile (insanely high rep, wow) and noticed this answer: http://stackoverflow.com/questions/4324133/javascript-garbage-collection/4324162#4324162. Will read! – Dmitry Minkovsky Feb 18 '14 at 18:01
  • @dimadima: You're right, everything in JavaScript is passed or assigned by value. There are is no pass-by-reference or assign-by-reference. There is one kind of value, called an *object reference*, that refers to objects. But it's still a value. It's just like the index into a big lookup table where objects live. Whether an implementation uses reference counting or other techniques doesn't matter, because you're just getting the value of `o1.someKey`, not any kind of reference to it. – T.J. Crowder Feb 18 '14 at 18:02