I'm worried about the memory increase.
If I use require as below, does the class create memory every time?
------------ a.js -------------`
class a {
constructor(){
}
}
module.exports = new a();
---------------
const IsMemoryIncrease = require('a');
Once a module is loaded, it is cached by the system. Subsequent attempts to load that module again just return the exact same module.exports that was previously created. The module initialization code is not run again.
So, in your case, every module that loads a.js will get the exact same instance of the a class. There will only be one instance.
Run this
// b.js
const IsMemoryIncrease1 = require('./a');
const IsMemoryIncrease2 = require('./a');
console.log(IsMemoryIncrease1);
console.log(IsMemoryIncrease2);
console.log(IsMemoryIncrease2 == require('./a'));