By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Is dynamic import useful in node.js?
JavaScript

Javascript – Is dynamic import useful in node.js?

Admin
Last updated: 2023/12/20 at 4:35 PM
Admin
Share
3 Min Read
Is dynamic import useful in node.js?

Problem:

I’m fairly new to node.js/express and I really like it so far.

Contents
Problem:Solution:

I use ES6 Syntax for imports.

When i started my project, i created all my routes like this :

app.get("/getmenu", (req, res) => {
  import("./js/get/getmenu.js").then((result) => {
    result.getMenus(req).then((menu) => {
      res.json(menu);
    })
  });
});

With all the logic and database call in the getMenu() function (store in getmenu.js file).

It works well, however, now that I understand better how things work, i start asking myself if dynamic import are any useful in this case? In node in general?

Wouldn’t it be easier to maintain and more performant to go with something like this :

import {getMenuRoute} from "./routes/getmenuroute.js";
...
app.get("/getmenu", getMenuRoute);

With all the logic and database call in the getMenuRoute() function in the dedicated file.

As far as i understand, import are only made once anyway. By doing them dynamic, I only speed up the server starting? But delay the first request to this route? Imported functions are stored somewhere in memory, do I avoid a global variable creation with my dynamic route? Does it matter?

Solution:

…i start asking myself if dynamic import are any useful in this case?

…

Wouldn’t it be easier to maintain and more performant to go with something like this :

import {getMenuRoute} from "./routes/getmenuroute.js";
...
app.get("/getmenu", getMenuRoute);
With all the logic and database call in the getMenuRoute() function in the dedicated file.

That’s a matter of opinion, but in the given example, I suspect most people would agree that no, the dynamic import doesn’t do anything useful for you there (but keep reading, depends how many routes you have).

As far as i understand, import are only made once anyway.

Right.

By doing them dynamic, I only speed up the server starting? But delay the first request to this route?

Right. When you import a module, the module’s top-level code is executed. If you have enough of them, that can delay server startup, in two ways:

  1. Executing the code, and
  2. Allocating memory for structures that code creates

…do I avoid a global variable creation with my dynamic route?

No, there’s no global variable creation in either case. Top-level identifiers in modules are not globals.

In node in general?

It’s definitely useful in general:

  1. If you have hundreds or even thousands of routes, dynamically importing them on first use can reduce server startup time (as you said).

  2. If you need to pick among different imports on the basis of the environment (development vs. staging vs. production, other characteristics of the environment), dynamic import is useful for only importing the code you’re actually going to run, and not the code for other environments.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How does a method of a map apply to each element when using something like .index of Javascript – How does a method of a map apply to each element when using something like .index of
Next Article Add multiple range filters on Jquery Data table Javascript – Add multiple range filters on Jquery Data table
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?