Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7399189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:01:36+00:00 2026-05-29T04:01:36+00:00

I was wondering if using require() in node.js was the equivalent to lazy loading?

  • 0

I was wondering if using require() in node.js was the equivalent to lazy loading?

For example if I had a function that required a specific node.js package that wasn’t needed anywhere else in my code am I best to use require() inside of that function to include the needed package only when that function is called.

I’m also unsure if this will provide any performance improvements given my lack of understanding around the node.js architecture? I presume it will use less memory per connection to my server. However will it increase I/O to the disk when it has to read the package, or will this be a one off to get it in memory?

If this is the case how far should I take this, should I be trying to write node.js packages for as much code as I can?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:01:36+00:00Added an answer on May 29, 2026 at 4:01 am

    require() is on-demand loading. Once a module has been loaded it won’t be reloaded if the require() call is run again. By putting it inside a function instead of your top level module code, you can delay its loading or potentially avoid it if you never actually invoke that function. However, require() is synchronous and loads the module from disk so best practice is to load any modules you need at application start before your application starts serving requests which then ensures that only asynchronous IO happens while your application is operational.

    Node is single threaded so the memory footprint of loading a module is not per-connection, it’s per-process. Loading a module is a one-off to get it into memory.

    Just stick with the convention here and require the modules you need at the top level scope of your app before you start processing requests. I think this is a case of, if you have to ask whether you need to write your code in an unusual way, you don’t need to write your code in an unusual way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a node.js script that does some logging to a file using WriteStream.
I like using Selenium RC for automating interaction with webpages that require JavaScript, except
I was wondering whether using a Belt and Braces (Suspenders) approach to programming -
I was wondering if using HTML entities in meta tags (like keywords and description)
I'm wondering if using the following Javascript code is reliable: if (!document.cookie) { alert('Cookies
I've been wondering if using long descriptive variable names in WinForms C# matters for
I am wondering about using the abstraction in Zend Db RowSet instead of joins,
I was wondering if when using the datagridview control you can remove the thing
Wondering if anybody out there has any success in using the JDEdwards XMLInterop functionality.
wondering if some of you know what javascript framework facebook is using ? Thanks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.