Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
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');
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Run this

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

console.log(IsMemoryIncrease1);
console.log(IsMemoryIncrease2);
console.log(IsMemoryIncrease2 == require('./a'));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda