Are let
and const
faster than var
in JavaScript?
ES6 introduced let
and const
as alternatives to var
for declaring variables in JavaScript. Because const
is not hoisted to the top of the execution context in the same way var
is, const should be faster, right?
Execution context and hoisting
The first lecture in Tyler McGinnisās Advanced JavaScript course covers execution context and hoisting, and it got me wondering about instantiating variables using var
and const
in JavaScript.
After the lecture, I was left thinking that the JavaScript keyword var
might be slower than the keyword const
, because var
needs to be hoisted.
Perhaps that extra step in the execution context hurts performance. If so, we would also expect to find that let
is slower than var
.
(If youāre not sure about the difference between each of these keywords for declaring variables, Iāve previously compared let
, var
, and const
).
Thereās evidence that many modern JavaScript features are slower than older JavaScript code. After all, ES6 (ECMAScript 6) was released in 2015 (and thus is also called ES2015). Prior to that, we were all using ES5 ā and many developers still do in order to support older browsers.
Kevin Decker has an amazing chart comparing the performance of ES6 features to ES5 baselines. You may be familiar with for...of
loops being slower than old-fashioned for
loops in JavaScript, for example:

Newer browser versions have generally reduced but not eliminated the performance gap that exists for ES6 features.
The idea that const
is actually slower than var
because of hoisting seems plausible, so I decided to test the hypothesis myself.
Wait, what is hoisting?
Hoisting is the part of the creation phase of the JavaScript execution context where variable declarations are assigned undefined
.
In creating the execution context, the JavaScript interpreter assigns variable declarations a default value of undefined
while placing any function declarations in memory. The variable references then already exist when theyāre used later.
Basically, the JavaScript interpreter looks for var
and function
keywords and āhoistsā those variables by assigning them the value of undefined
.
But does this mean that there is an extra step needed when using var
to declare a variable instead of using the ES6 keywords let
or const
? Shouldnāt that make let
and const
slower than var
?

Why const
could be faster
It appears that using const
would inherently make code a little faster, because it seems to reduce the amount of hoisting necessary.
Take the following, basic example:
var greeting = "Hello world!"
When executed using var
, the code is interpreted in the following way because of hoisting (though no code is actually moved by the interpreter):
var greeting // undefined
greeting = "Hello world!"
So, it stands to reason that quite possibly the following might be faster:
const greeting = "Hello world!"
While it appears trivial, if let
and const
are actually faster, then that would be a strong argument for consistently using them. We would also want to actively refactor old code currently using var
.
Hoisting works similarly for functions
In the case of creating a function, hoisting works similarly in JavaScript. When using the function
keyword, the code is treated like var
:
function reverseString(string) { return string.split("").reverse().join("") }
Again, the function declaration is hoisted, effectively creating:
var reverseString
reverseString = function (string) { return string.split("").reverse().join("") }
Compare that to using a const
and an anonymous function declared with the arrow syntax:
const reverseString = (string) => string.split("").reverse().join("")
Again, it may not make any difference at all, but doesnāt it seem that fewer steps for the compiler would mean faster performance?
The test results
To find out if the ES6 keyword const
is faster than using var
, I ran test cases using jsPerf, a free JavaScript performance testing tool that runs in the browser.
Authorās note: Unfortunately, jsPerf has gone down.
Users interested in micro-benchmarking using their own setups are advised to try https://jsbench.me/ or https://jsben.ch/ as alternatives.
I reasoned that only using const
would make for a clearer test. Intuitively, it seemed that constant declarations would somehow be faster than variable declarations, so const
would be faster than var
.
Since hoisting is used for both var
variables and function
functions, I wanted to test the performance of function declarations as well.
Depending on the results, I would go back to compare let
and var
, taking a look at any found differences in more detail.
Test 1 āVariables only

There was no difference in performance when instantiating five variables, including a string concatenation using backtick literals.
We see right off the bat that const
and var
have identical performance, at least on my Windows 7 machine running Firefox (x64).
Test 2 ā functions only

Functions also showed identical performance, as we can see for the reverseString
function in the above test case.
Just like we saw for variables, there was no difference in performance for functions declared as const
variables using the arrow syntax compared to those declared using the traditional function
keyword syntax.
Third ā variables and function

Combining both code snippets produced the same result: there was no difference in performance between using var
and const
. I decided not to test let
in detail, since the performance was identical.
A valid criticism of the above test cases is that calling console.log()
slows down the code execution, possibly hiding any effects of hoisting.
Indeed, tests 2 and 3 each have a pair of console.log()
statements, instead of just the single console.log()
call in test 1. And, as we might expect, tests 2 and 3 run about 50% slower than test 1.
If the performance difference between const
vs. var
was large enough to worry about, I would think it would still show up. However, I also ran similar tests without console.log()
using Chrome, and the results were the same: there was no difference between const
and var
.
That said, you may want to remove console.log()
statements when youāre conducting your own microperformance testing.

What it means
Some newer ECMAScript features are definitely faster than previous implementations, like the .padStart()
and .padEnd()
string padding methods that were added in ES2018. My testing on string padding performance showed .padStart()
was actually 10x faster.
Other newer features are slower than the older versions, like the aforementioned for...of
loops. For variable declaration at least, there is no performance boost changing from var
(ES5) to const
(ES6).
Under the hood, JavaScript is treating var
, let
, and const
variables the same, except for their important differences in lexical scope (that I explained previously in my article in JavaScript in Plain English).
Unlike the padding algorithms noted above, which deliver a performance benefit, thereās no performance benefit (or penalty) for choosing to use let
and const
instead of var
and function
.
The execution context underlying how the JavaScript interpreter runs the code is basically the same when you use var
compared to when you use let
and const
. That results in the same execution speed.
Comparing const
to compiling
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).
Meanwhile, a brand-new feature like string padding has the potential to be much faster than the old way of doing things.
To put it in context, letās compare compiling vs. polyfilling:
- Using a different keyword like
let
orconst
still instantiates a new variable in JavaScriptās execution context in much the same way thatvar
does (and thusconst
could be compiled with Babel). - Other features like string padding include new algorithms that may have better underlying performance (but string padding would need to be polyfilled to be compatible with older browsers).
Generally, compiling will be just as fast, but polyfilling may be slower.
To make a prediction about the speed of a new ECMAScript feature, you can think about it in terms of compiling and polyfilling:
- The newer feature is faster if it replaces a polyfill with native code, as with string padding. Itās like removing a polyfill.
- The older feature is faster if the newer feature adds extra code under the hood, as with
for...of
loops. Itās like adding a polyfill. - The features will be the same speed if there is no change in the amount of code to process. Itās like Babel compiling
let
tovar
ā no code was added, so the performance should be the same.
The last reason is exactly why I found ES6 backtick literals arenāt slower; theyāre the same speed as ES5 string concatenation.
Why to use const anyway
There are plenty of reasons to use let
and const
instead of var
, but worrying about performance hits from hoisting is not one of them.
These are the 4 advantages of let
and const
identified by Maya Shavin in Front End Weekly:
ā¢Avoid polluting our global object with unnecessary properties.
ā¢Avoid hidden š ā such as modifying a constant value by mistake, updating wrong variables which are in different scope block but declared with same name, etcā¦
ā¢Avoid unnecessary hoisting.
ā¢Add more restrictions to force our code [to be] more reliable, organized and easier to read
Another great reason to use const
is that using const
instead of var
tells the next developer that the given variable is meant to be a constant. Other than SCREEN_CASE, thereās not a great way to do that in JavaScript.
Conclusion: let and var walk into a barā¦
Variables declared with var
and functions declared with function
are hoisted to the top of the execution context and assigned a value of undefined
before any code is interpreted. In comparison, the ES6 keywords let
and const
are not hoisted.
Compared to using const
, there is no performance difference in declaring variables or functions with var
and function
, at least in my microperformance testing in a modern web browser.
Hoisting does not slow JavaScript down in any way ā and why should it? Itās one of the oldest concepts in JavaScript (though the term hoisting itself didnāt become part of the ECMASCript specification until ES6).
Nevertheless, using let
and const
is generally recommended to limit scope and mutability and therefore minimize bugs.
That being said, donāt forget that objects (including arrays) declared using const
can have their contents changed. Thatās the reason we need to deep copy nested objects and arrays, though a shallow copy of an array works great if the array doesnāt contain any other arrays or objects.
While microperformance testing is often trivial (for...of
loops being 1.6x slower doesnāt matter for 99% of codebases), I think itās a useful way of thinking critically about how JavaScript actually works. And developing critical thinking skills about the programming languages we use every day is something that helps all of us improve as developers.
If you enjoyed this article, you may enjoy reading about my surprising results when using the ...
spread operator to find the smallest and largest numbers in an array. I definitely wasnāt expecting what I found:
Iāve included additional resources below to help you master the essential JavaScript concepts of scope, execution context, and hoisting.
Happy coding! šš©āš»š»šØāš»š§
Further reading
- The ui.dev JavaScript Visualizer by Tyler McGinnis is a great tool for understanding execution context in JavaScript (ES5):
- Sukhjinder Arora differentiates execution context from the execution stack in his article in Bits & Pieces:
- Harrison Grant-Favor examines āgotchasā for hoisting and scope when using
var
,let
, andconst
in Noteworthy ā The Journal Blog:
- Gemhar Rafi delivers one of the clearest explanations of
let
,var
, andconst
that Iāve ever read in his piece on Dev.to:
- Jakub Janczyk explains immutability in the context of
const
by comparingconst
toObject.freeze()
in the Pragmatists blog: