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.
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.
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).
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.
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.
The const
makes perfect sense here:
let
) which also makes sense.Other than that it comes down to personal preference (using var
, let
or const
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With