home

A granter pattern for reducing the size of private instance methods in JavaScript

December 2025

I am not a programmer and my theoretical background is that of an amateur hobbyist (in my case quite lacking) and so I might be getting things completely wrong here, but the way I understand it is that when using classes in JavaScript, private instance methods take up more memory than public methods because the first are stored in each instance of the class (as a separate copy of that method that is accessible to that particular instance only), while the latter are only stored once in the prototype of the class and from there are available to each instance of the class and so do not add any extra weight to an instance.

I was working on this little thing the other day and for no real reason set out to make some method private but - again for no other reason than pure curiosity - was also considering the potential memory impact this would have in case many instances of its class would be created. So I was dabbling around on how to prevent an unnecessary increase of memory usage and I had this idea to minimize the size of the private method by having it only serve as a gateway that enables the execution of a code heavy public method, say of an event handler that should ideally be accessible only from within the class. Like so:


class SomeClass {
    #passGranted; // hash symbol denotes private elements (fields, methods)

    constructor() {
        this.#passGranted = false;
        // some code, including a callback named #granter
        // that is a proxy/gateway for some code heavy event handler;
        // ...addEventListener(someEvent, this.#granter.bind(this));
    }

    #granter() {
        this.#passGranted = true;
        this.someCodeHeavyEventHandler();
    }

    someCodeHeavyEventHandler() {
        if(this.#passGranted) {
            this.#passGranted = false;
            // considerable amount of code
        }
    }
}

So someCodeHeavyEventHandler() is public and therefore optimized in the sense of memory management, but its execution is as if it were not a public method, since it is not possible to run it at will from outside of the class as the pass to run it (#passGranted, a private variable that cannot be changed from outside the class) is granted in a private method that is only called in reposnse to a predefined event (someEvent). Sort of the best of both worlds (if I understand correctly). Nothing big really, and nothing new, but within the arc of my journey with learning to program, this was a solution I came up with by myself and have not learned about elsewhere. In my hobbyist work such moments of eureka are really like the best thing there is. When you set out to solve a conundrum and succeed in coming up with an effective and efficient solution it feels like a small victory and the feeling this brings is really like the best thing there is.

It's also quite interesting how our brains work. While working to solve this thing, I remember at a certain point I recalled the singleton pattern from Java, which is sort of a similar concept (lots of beating around the bush to get to a thing, but in this way making sure to keep it secure), and started thinking about it and some minutes later this solution came to my mind. It is as if while you are mulling about something the brain combines 1 + 1 in the background and then floats a solution to your consciousness. Quite a beautiful and interesting experience.



Ivo Makuc, 2025
byguesswork@gmail.com