JavaScript Is it a bad practice to use `require` inside a constructor in NodeJS?

I am building a node app where a certain wordlist is required. The wordlist is in a JSON file that looks something like this:

 { 
   "en":["foo", "bar"],
   "gr": ["foo", "bar"]
 }

Each key inside of the JSON file represents a different language.

The user has to pick a language when they create their object. So I am thinking of importing the JSON file inside of the constructor like this:

const list = require('./config/lang.json')[lang]

Where lang is a parameter passed to the constructor.

Is this bad practice?

I've heard people say that you should always use require in the beginning of your code. Should I just require the whole thing in the beginning of my code const list = require('./config/lang.json') and then simply extract only the required language const wordlist = list[lang]inside the constructor?

Answer:1

Even though the code works the same, and require calls are cached. In your case there is no need for that extra function call on every new instance, so it will be faster (not that it matters in this case) to do the following:

const langs = require('./config/lang.json');

class MyClass {
    constructor(lang) {
        const list = langs[lang];
    }
}

Another thing to notice, is that require is synchronous, so if your JSON is specially large, the first time you instanciate MyClass the event loop will be blocked. Using it at the very beginning it will probably have loaded before the server (or whatever you're doing) has started, so there would be no issue in the require taking some time.

So, yes, in my opinion, require calls should be at the top, unless you know what you're doing, or you're loading a dynamic dependency.

Answer:2

im looking for the elegant way to avoid writing so much code to perform onclick, show clicked, hide others. here's the code im using: html: <p align="center" style="font-size: 22px;"> <...

im looking for the elegant way to avoid writing so much code to perform onclick, show clicked, hide others. here's the code im using: html: <p align="center" style="font-size: 22px;"> <...

I apply inheritance in JavaScript in following way: var employee = function(name) { this.name = name; } employee.prototype.getName = function() { return this.name; } var pEmployee = ...

I apply inheritance in JavaScript in following way: var employee = function(name) { this.name = name; } employee.prototype.getName = function() { return this.name; } var pEmployee = ...

  1. inherit javascript function
  2. javascript inheritance function override
  3. odoo inherit javascript function
  4. javascript inheritance extend function
  5. javascript inheritance super function
  6. javascript function inheritance prototype
  7. javascript inheritance call parent function
  8. javascript inheritance function call

I'm using angular-ui-tree in order to render a tree with data but each time that the page is loaded all nodes are rendered and I would like to avoid the render because with huge data locks the browser....

I'm using angular-ui-tree in order to render a tree with data but each time that the page is loaded all nodes are rendered and I would like to avoid the render because with huge data locks the browser....

  1. angular avoid tree

I am trying to get the value of some array elements. It works for the elements [0], [1], [2], [3], but not [4]. function getBase64() { const urls = ['https://i.imgur.com/egNg7JU.jpg', 'https://...

I am trying to get the value of some array elements. It works for the elements [0], [1], [2], [3], but not [4]. function getBase64() { const urls = ['https://i.imgur.com/egNg7JU.jpg', 'https://...

  1. javascript wait array of promises
  2. javascript wait array
  3. javascript wait until array is filled
  4. javascript wait for array to be populated
  5. javascript wait for array of promises
  6. js wait for array of promises