Reclaiming memory from Nodes

If you are interested in where I've got to in deconstructing the uibuilder runtime, please check out the v4.2.0-dev branch. It is quite interesting but I'm not sure I would recommend it except for complex nodes as it does need some advanced features of node.js. However, I have managed to eliminate the node variable completely. The exported function is only a few lines long now as all the work is done in separate functions. This has also resulted in some simplification of the code and the removal of a few pieces of odd logic.

The only reason this was a fairly straightforward task (an evening) was because I already saved all of the important module-level data in variables outside the exported function. This meant that they were still available when the functions were split out.

The only exeption to that is the RED variable which is the reference to the Node-RED runtime object. I could actually further simplify the code if I saved a reference to RED. Something I might try.

https://github.com/TotallyInformation/node-red-contrib-uibuilder/blob/v4.2.0-dev/nodes/uibuilder.js

Well now that I have destructured the code, I think that I can see how a node might be converted to a class. But I am far from sure that it would have any runtime benefits.

I actually don't think that applies to nodes. In reality, only a single copy of the module that defines a node is loaded. It is only the function passed to registerType that is run more than once - for each instance of a node.

I went down the singleton route myself. web.js, socket.js and security.js are now all singleton class modules. Means that I don't have to fret over static elements. I really like how clean that code is now and far more comprehensible and easier to debug - though I have had to wrap the requires in try/catch statements so that I get the correct source line if there is an error in the class. Even the "disadvantage" of the singleton model (you can't pass parameters to the constructor) is an advantage in my case because I don't want to (can't actually) fully set-up the class instance when it is require'd, that has to be done later on and I just use another class method.

It has been quite a journey of understanding getting into the depths of classes in JavaScript and exactly what they are and are not. I've also learned a lot more about the this object and arrow functions.

There is certainly more to come. I really need to deal with the messy uiblib.js file which is a random collection of utility functions. Most of which get passed an inordinate number of parameters!

Thanks a ton @TotallyInformation ... Apologies I got occupied with SSO implementation in my project using Okta & Keycloak....cud not get to look into this. However will soon revert back to you.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.