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

182
Vistas
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 Respuestas
Responde la pregunta

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 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