Nodejs: Difference between requests (require)

Asked

Viewed 99 times

2

I started learning Nodejs and noticed that there are some ways to request a file, two of them are:

const app = require('lib').app

const {app} = require('lib')

Is there any difference between them in relation to performance or are both equal? A friend told me to have seen somewhere (who does not remember) that one of them is heavier and can influence the performance whenever the file is executed.

1 answer

3


Both are equal.

Use const {app} = require('lib') is a new tool that has been implemented in ES6 version, called Destructuring assignemt and that was not possible in the past. That is, before you had to make an assignment for each property.

In your case it makes little difference but if require('lib') export more properties you can do all in one line:

const {app, router, middleware} = require('lib')
  • 1

    Got it! Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.