NodeJS: Difference between requisitions (require)

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 performance or are they both the same? A friend told me that he saw somewhere (which he does not remember) that one is heavier and can influence performance every time the file is run.

    
asked by anonymous 11.08.2017 / 18:11

1 answer

2

Both are the same.

Using const {app} = require('lib') is a new tool that was implemented in ES6, called Destructuring assignemt and that was not possible before. That is, before you had to do 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')
    
11.08.2017 / 18:15