A Node crisis of confidence
After over ten years of PHP, I’ve spent the last couple of years almost exclusively developing using Node.JS. I consider myself pretty proficient. Happy with prototypal inheritance, Promises, and much of the new ES6 stuff.
However, today I started to get the feeling that I’d been doing something wrong for a long time. It concerns the sharing of resources across modules.
The question being, should a shared resource be required once and passed to modules, or can we rely on Node’s caching (and pseudo-singleton implementation) to prevent duplicating resources. Consider the following two approaches:
In the first approach, only one instance of
db is ever created and used. This is explicitly clear. Though it does introduce dependency injection. The module itself cannot stand alone.
In the second approach, the
db module is loaded twice, though because it’s cached by Node, the second time gets the same instance as the first time. But the node developers acknowledge this is not guaranteed. If this fails, memory consumption could shoot up and database connections could grind to a halt.
I much prefer the second approach, the one I’ve used for two years without incident. But now I’m concerned it may be the wrong approach for larger projects.
If you’ve any thoughts, contact me on Twitter.
After reading Liam Kaufman’s article, How AngularJS Made Me a Better Node.js Developer and this thread on Stack Overflow I’ve come to the conclusion that the answer to the above is, it depends. Both approaches are equally valid and have different use-cases. I’d still be keen to see if there is any performance benefit to one approach over the other.