Search code examples
javascriptmemory-leaksprototypal-inheritance

Prototypal Inheritance Issues in JavaScript


I have some questions about my prototypal inheritance pattern that I am using for my app. First of all take a look at the pattern that I am using;

const myObjectProto = (function() {

  const proto = Object.create( parentProto );

  // private variables
  let title = 'Some String';

  // private functions
  const addEmoticon = function() {
    return title + ' ';
  }

  proto.getTitle = function() {
    return addEmoticon();
  }

  return proto;
})();

And when I want to use it, I will use it like below:

const firstObject = Object.create( myObjectProto );
const secondObject = Object.create( myObjectProto );

// call a public function
firstObject.getTitle();
secondObject.getTitle();

My first question is this: assume that we are creating 100 objects from its corresponding proto; As far I know title is shared among them. That means if a function change it, it will be changed for all created objects. I am a little worried about the memory in this case. Do I use memory efficient with this pattern? Can someone clarify how Object.create treats with title?

My second question is about the memory leak; Let say that we want to set the firstObject to null;

firstObject = null;

Based on the fact that we have shared variables like title and even proto, I think it does nothing and I have memory leak. Am I right? If this is true, how can I fix it?


Solution

  • title is a local variable. It will exist as long as some code can access it. It will only exists once, as the IIFE only executes once, thus only one variable gets created. The function inside addEmoticon can access title, in other words it closures over it. As it is a local itself, it will also only exist as long as it can be accessed. It gets accessed inside the function stored inside proto.getTitle, which again closures over addEmoticon. proto.getTitle exists as long as proto can be accessed. As the value of proto gets stored inside the global variable const myObjectProto, it can always be accessed thus it will live forever (as well as every value that can be accessed through it, e.g. title).

    Do I use memory efficient with this pattern?

    Well, you allocate a few bytes once. Assuming that you have a few Megabytes available, thats not a lot.

    As far I know title is shared among them

    Yes.

    Based on the fact that we have shared variables like title and even proto, I think it does nothing and I have memory leak.

    You loose the reference to the instance stored in firstObject, thus making it viable for garbagge collection. That might free a few bytes. As the prototype is still available via secondObject and myObjectProto, it will continue living.

    I have memory leak. Am I right?

    A memory leak is a noticible amount of memory, that is still allocated although the values in it don't need to be used anymore. Under the assumption that you want to work with secondObject, and create new instances from myObjectProto, everything you've stored is still needed. Your code is perfectly fine.