Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda