Is there an efficiency difference between const vs var while 'requiring' a module in NodeJS

node.js

node.js Problem Overview


I was reading the documentation for https://github.com/rvagg/bl and I noticed that, in the examples, they use const to require a module and this made me wonder: is this a good practice? I mean, to me, this looked as a good idea.

A direct example from the link above is:

const BufferList = require('bl')

var bl = new BufferList()
bl.append(new Buffer('abcd'))
bl.append(new Buffer('efg'))
/*...*/

I also noticed the lack the semicolons in the example but well, that has been discussed elsewhere thoroughly.

node.js Solutions


Solution 1 - node.js

The const makes perfect sense here:

  • It documents that the object reference is not going to change.
  • It has block scope (same as let) which also makes sense.

Other than that it comes down to personal preference (using var, let or const)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHugoView Question on Stackoverflow
Solution 1 - node.jshelpermethodView Answer on Stackoverflow