Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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.

like image 701
Hugo Avatar asked May 06 '14 00:05

Hugo


People also ask

Which is better const or VAR?

var variables can be updated and re-declared within its scope; let variables can be updated but not re-declared; const variables can neither be updated nor re-declared. They are all hoisted to the top of their scope. But while var variables are initialized with undefined , let and const variables are not initialized.

Is const faster than VAR?

The reason const is not faster than var is that const is not really a new feature, as JavaScript of course had variables all along. A minor change in lexical scope really doesn't affect anything regarding performance, even with hoisting (or the lack there-of).

Why are const better than VAR?

The scope of a let variable is block scope. The scope of a const variable is block scope. It can be updated and re-declared into the scope. It can be updated but cannot be re-declared into the scope.

Is let more efficient than VAR?

After testing this in Chrome and Firefox, this shows that let is faster than var , but only when inside a different scope than the main scope of a function. In the main scope, var and let are roughly identical in performance. In IE11 and MS Edge, let and var are roughly equal in performance in both cases.


1 Answers

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)

like image 91
helpermethod Avatar answered Sep 19 '22 03:09

helpermethod