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

181
Visualizações
Is there a benefit to destructuring when using require?

Is there any performance advantage or disadvantage (when using require) to importing the entire module, versus importing only selected functions?

As I understand it when using require to import modules (as opposed to using import ) compilers such as Babel do not perform any pruning of the final code as it does when using import. As such, considering then that both methods end up providing pointers to functions. I'm wondering if there is any reason to prefer one over the other. Or if this is just a matter of personal preference and readability.

const { methodOne, methodTwo } = require('../myLibrary')
const x1 = methodOne()
const x2 = methodTwo()

VS

const Lib = require('../myLibrary')
const x1 = Lib.methodOne()
const x2 = Lib.methodTwo()

I'm not looking for opinions on one style versus the other, I think both have value in certain situations. I'm just trying to figure out if there even is a technical reason to prefer one over the other.
Also for now let's just assume I'm not using Node 14 so import is not available.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Is there any performance advantage or disadvantage (when using require) to importing the entire module, versus importing only selected functions?

No. The entire module is loaded either way. It's only a matter of which exports you choose to save within your module so you can use them.

As I understand it when using require to import modules (as opposed to using import ) compilers such as Babel do not perform any pruning of the final code as it does when using import.

Correct. There is no pruning with require() or with import. Basically nodejs doesn't do pruning - entire modules are loaded or not. Pruning would have to be done by something like a bundler or packager that can rewrite/reorganize the code before it is fed to nodejs and physically remove code that isn't being referenced.

As such, considering then that both methods end up providing pointers to functions. I'm wondering if there is any reason to prefer one over the other. Or if this is just a matter of personal preference and readability.

Just personal preference.

I'm not looking for opinions on one style versus the other, I think both have value in certain situations. I'm just trying to figure out if there even is a technical reason to prefer one over the other.

No technical reason to prefer one over the other.

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