Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If module.exports creates a new class, is it newly created if called with require? Or does it only work with one? in node.js

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');

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Run this

// b.js
const IsMemoryIncrease1 = require('./a');
const IsMemoryIncrease2 = require('./a');

console.log(IsMemoryIncrease1);
console.log(IsMemoryIncrease2);
console.log(IsMemoryIncrease2 == require('./a'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading