TL;DR

In theory, an unoptimized version of this loop:

for (let i = 0; i < 500; ++i) {
    doSomethingWith(i);
}

might be slower than an unoptimized version of the same loop with var:

for (var i = 0; i < 500; ++i) {
    doSomethingWith(i);
}

because a different i variable is created for each loop iteration with let, whereas there's only one i with var.

Arguing against that is the fact the var is hoisted so it's declared outside the loop whereas the let is only declared within the loop, which may offer an optimization advantage.

In practice, here in 2018, modern JavaScript engines do enough introspection of the loop to know when it can optimize that difference away. (Even before then, odds are your loop was doing enough work that the additional let-related overhead was washed out anyway. But now you don't even have to worry about it.)

Beware synthetic benchmarks as they are extremely easy to get wrong, and trigger JavaScript engine optimizers in ways that real code doesn't (both good and bad ways). However, if you want a synthetic benchmark, here's one:

const now = typeof performance === "object" && performance.now
    ? performance.now.bind(performance)
    : Date.now.bind(Date);

const btn = document.getElementById("btn");
btn.addEventListener("click", function() {
    btn.disabled = true;
    runTest();
});

const maxTests = 100;
const loopLimit = 50000000;
const expectedX = 1249999975000000;

function runTest(index = 1, results = {usingVar: 0, usingLet: 0}) {
    console.log(`Running Test #${index} of ${maxTests}`);
    setTimeout(() => {
        const varTime = usingVar();
        const letTime = usingLet();
        results.usingVar += varTime;
        results.usingLet += letTime;
        console.log(`Test ${index}: var = ${varTime}ms, let = ${letTime}ms`);
        ++index;
        if (index <= maxTests) {
            setTimeout(() => runTest(index, results), 0);
        } else {
            console.log(`Average time with var: ${(results.usingVar / maxTests).toFixed(2)}ms`);
            console.log(`Average time with let: ${(results.usingLet / maxTests).toFixed(2)}ms`);
            btn.disabled = false;
        }
    }, 0);
}

function usingVar() {
    const start = now();
    let x = 0;
    for (var i = 0; i < loopLimit; i++) {
        x += i;
    }
    if (x !== expectedX) {
        throw new Error("Error in test");
    }
    return now() - start;
}

function usingLet() {
    const start = now();
    let x = 0;
    for (let i = 0; i < loopLimit; i++) {
        x += i;
    }
    if (x !== expectedX) {
        throw new Error("Error in test");
    }
    return now() - start;
}
<input id="btn" type="button" value="Start">

It says that there's no significant difference in that synthetic test on either V8/Chrome or SpiderMonkey/Firefox. (Repeated tests in both browsers have one winning, or the other winning, and in both cases within a margin of error.) But again, it's a synthetic benchmark, not your code. Worry about the performance of your code when and if your code has a performance problem.

As a style matter, I prefer let for the scoping benefit and the closure-in-loops benefit if I use the loop variable in a closure.

Details

The important difference between var and let in a for loop is that a different i is created for each iteration; it addresses the classic "closures in loop" problem:

function usingVar() {
  for (var i = 0; i < 3; ++i) {
    setTimeout(function() {
      console.log("var's i: " + i);
    }, 0);
  }
}
function usingLet() {
  for (let i = 0; i < 3; ++i) {
    setTimeout(function() {
      console.log("let's i: " + i);
    }, 0);
  }
}
usingVar();
setTimeout(usingLet, 20);

Creating the new EnvironmentRecord for each loop body (spec link) is work, and work takes time, which is why in theory the let version is slower than the var version.

But the difference only matters if you create a function (closure) within the loop that uses i, as I did in that runnable snippet example above. Otherwise, the distinction can't be observed and can be optimized away.

Here in 2018, it looks like V8 (and SpiderMonkey in Firefox) is doing sufficient introspection that there's no performance cost in a loop that doesn't make use of let's variable-per-iteration semantics. See this test.


In some cases, const may well provide an opportunity for optimization that var wouldn't, especially for global variables.

The problem with a global variable is that it's, well, global; any code anywhere could access it. So if you declare a variable with var that you never intend to change (and never do change in your code), the engine can't assume it's never going to change as the result of code loaded later or similar.

With const, though, you're explicitly telling the engine that the value cannot change¹. So it's free to do any optimization it wants, including emitting a literal instead of a variable reference to code using it, knowing that the values cannot be changed.

¹ Remember that with objects, the value is a reference to the object, not the object itself. So with const o = {}, you could change the state of the object (o.answer = 42), but you can't make o point to a new object (because that would require changing the object reference it contains).


When using let or const in other var-like situations, they're not likely to have different performance. This function should have exactly the same performance whether you use var or let, for instance:

function foo() {
    var i = 0;
    while (Math.random() < 0.5) {
        ++i;
    }
    return i;
}

It's all, of course, unlikely to matter and something to worry about only if and when there's a real problem to solve.

Answer from T.J. Crowder on Stack Overflow
Top answer
1 of 6
184

TL;DR

In theory, an unoptimized version of this loop:

for (let i = 0; i < 500; ++i) {
    doSomethingWith(i);
}

might be slower than an unoptimized version of the same loop with var:

for (var i = 0; i < 500; ++i) {
    doSomethingWith(i);
}

because a different i variable is created for each loop iteration with let, whereas there's only one i with var.

Arguing against that is the fact the var is hoisted so it's declared outside the loop whereas the let is only declared within the loop, which may offer an optimization advantage.

In practice, here in 2018, modern JavaScript engines do enough introspection of the loop to know when it can optimize that difference away. (Even before then, odds are your loop was doing enough work that the additional let-related overhead was washed out anyway. But now you don't even have to worry about it.)

Beware synthetic benchmarks as they are extremely easy to get wrong, and trigger JavaScript engine optimizers in ways that real code doesn't (both good and bad ways). However, if you want a synthetic benchmark, here's one:

const now = typeof performance === "object" && performance.now
    ? performance.now.bind(performance)
    : Date.now.bind(Date);

const btn = document.getElementById("btn");
btn.addEventListener("click", function() {
    btn.disabled = true;
    runTest();
});

const maxTests = 100;
const loopLimit = 50000000;
const expectedX = 1249999975000000;

function runTest(index = 1, results = {usingVar: 0, usingLet: 0}) {
    console.log(`Running Test #${index} of ${maxTests}`);
    setTimeout(() => {
        const varTime = usingVar();
        const letTime = usingLet();
        results.usingVar += varTime;
        results.usingLet += letTime;
        console.log(`Test ${index}: var = ${varTime}ms, let = ${letTime}ms`);
        ++index;
        if (index <= maxTests) {
            setTimeout(() => runTest(index, results), 0);
        } else {
            console.log(`Average time with var: ${(results.usingVar / maxTests).toFixed(2)}ms`);
            console.log(`Average time with let: ${(results.usingLet / maxTests).toFixed(2)}ms`);
            btn.disabled = false;
        }
    }, 0);
}

function usingVar() {
    const start = now();
    let x = 0;
    for (var i = 0; i < loopLimit; i++) {
        x += i;
    }
    if (x !== expectedX) {
        throw new Error("Error in test");
    }
    return now() - start;
}

function usingLet() {
    const start = now();
    let x = 0;
    for (let i = 0; i < loopLimit; i++) {
        x += i;
    }
    if (x !== expectedX) {
        throw new Error("Error in test");
    }
    return now() - start;
}
<input id="btn" type="button" value="Start">

It says that there's no significant difference in that synthetic test on either V8/Chrome or SpiderMonkey/Firefox. (Repeated tests in both browsers have one winning, or the other winning, and in both cases within a margin of error.) But again, it's a synthetic benchmark, not your code. Worry about the performance of your code when and if your code has a performance problem.

As a style matter, I prefer let for the scoping benefit and the closure-in-loops benefit if I use the loop variable in a closure.

Details

The important difference between var and let in a for loop is that a different i is created for each iteration; it addresses the classic "closures in loop" problem:

function usingVar() {
  for (var i = 0; i < 3; ++i) {
    setTimeout(function() {
      console.log("var's i: " + i);
    }, 0);
  }
}
function usingLet() {
  for (let i = 0; i < 3; ++i) {
    setTimeout(function() {
      console.log("let's i: " + i);
    }, 0);
  }
}
usingVar();
setTimeout(usingLet, 20);

Creating the new EnvironmentRecord for each loop body (spec link) is work, and work takes time, which is why in theory the let version is slower than the var version.

But the difference only matters if you create a function (closure) within the loop that uses i, as I did in that runnable snippet example above. Otherwise, the distinction can't be observed and can be optimized away.

Here in 2018, it looks like V8 (and SpiderMonkey in Firefox) is doing sufficient introspection that there's no performance cost in a loop that doesn't make use of let's variable-per-iteration semantics. See this test.


In some cases, const may well provide an opportunity for optimization that var wouldn't, especially for global variables.

The problem with a global variable is that it's, well, global; any code anywhere could access it. So if you declare a variable with var that you never intend to change (and never do change in your code), the engine can't assume it's never going to change as the result of code loaded later or similar.

With const, though, you're explicitly telling the engine that the value cannot change¹. So it's free to do any optimization it wants, including emitting a literal instead of a variable reference to code using it, knowing that the values cannot be changed.

¹ Remember that with objects, the value is a reference to the object, not the object itself. So with const o = {}, you could change the state of the object (o.answer = 42), but you can't make o point to a new object (because that would require changing the object reference it contains).


When using let or const in other var-like situations, they're not likely to have different performance. This function should have exactly the same performance whether you use var or let, for instance:

function foo() {
    var i = 0;
    while (Math.random() < 0.5) {
        ++i;
    }
    return i;
}

It's all, of course, unlikely to matter and something to worry about only if and when there's a real problem to solve.

2 of 6
31

"LET" IS BETTER IN LOOP DECLARATIONS

With a simple test (5 times) in navigator like that:

// WITH VAR
console.time("var-time")
for(var i = 0; i < 500000; i++){}
console.timeEnd("var-time")

The mean time to execute is more than 2.5ms

// WITH LET
console.time("let-time")
for(let i = 0; i < 500000; i++){}
console.timeEnd("let-time")

The mean time to execute is more than 1.5ms

I found that loop time with let is better.

🌐
Overreacted
overreacted.io › on-let-vs-const
On let vs const — overreacted
However, in many codebases most variables won’t satisfy either of those cases, and parameters can’t be marked as constant at all. No Performance Benefits: It is my understanding that the engines are already aware of which variables are only assigned once — even if you use var or let.
🌐
Reddit
reddit.com › r/javascript › how much performance does using const with es6 gain you?
r/javascript on Reddit: How much performance does using const with ES6 gain you?
April 29, 2015 -

It seems that the general consensus of people writing ES6 is to use const for variables that won't change in value, which principally I can't disagree with.

But sometimes I wonder how much that really gains your application from a performance standpoint. If someone tries to reassign a const somewhere in the app that truly shouldn't be reassigned, the value there is clear. But if we're talking in terms of pure processing speed, does it make a difference?

Let's say in the worst-case scenario, I replaced all instances of const in your app with let (or even var), and run a million iterations of the most complex function affected by this. What would change? Would it run any slower? Any difference for server-side code vs. client-side?

Just wondering this from a curiosity standpoint. I'm not against const in any way, would just like to know what it gains you besides the type of bug prevention it can possibly give in other programming languages.

Top answer
1 of 5
15

Performance-wise, if whatever difference between let and const actually surfaces with meaningful impact then you're probably already in a very good spot.

const, however, is great for code readability because it is a statement of how the code is supposed to work. If I see a const variable I expect it not to be reassigned. If I see a let, I expect it to be reassigned. Anything else tells me there is likely a bug. Given that code clarity is almost always the greatest concern, using const as the default choice and let only when necessary makes a lot of sense to me.

2 of 5
4

It seems that the general consensus of people writing ES6 is to use const for variables that won't change in value, which principally I can't disagree with.

This is true. And it's a fairly good idea. (The benefits can be overstated. Keep in mind that const does not prevent, nor is it intended to prevent, people mutating complex values. const user = getUser(); user.name = 'Tom' is perfectly legal.)

But sometimes I wonder how much that really gains your application from a performance standpoint.

Absolutely, literally, nothing. That's not why people recommend it. I'd say that the benefits of const are basically 100% that it makes the code easier to write and (especially maintain) via marginal improvements to clarity and programmer intent. let tells me that the author is intending the re-assign a variable, which is good to know since that's pretty unusual. const tells me it's a "normal" variable that shouldn't be re-assigned, and my linter will helpfully flag it should I start to try (or if I try and re-declare it). Every so often I'll go "oh right, I already have a user object", and that's slightly helpful, and has no drawbacks, so...win?

I replaced all instances of const in your app with let (or even var), and run a million iterations of the most complex function affected by this. What would change? Would it run any slower?

Swap with let, and nothing would change at all; it certainly wouldn't run slower. Swap with var, and I suppose the different scoping might cause a bug and due to a couple of minor bugs in some browsers, it might even run faster.

🌐
GitHub
github.com › tailwindlabs › tailwindcss › discussions › 11351
Why using let instead of const? · tailwindlabs/tailwindcss · Discussion #11351
It's important to note that the use of let or const is not necessarily related to performance. Both keywords have similar performance characteristics, and choosing one over the other does not significantly impact the execution speed of your code.
Author   tailwindlabs
🌐
Medium
medium.com › @artemkhrenov › the-hidden-cost-of-modern-javascript-understanding-let-const-performance-f0732fba9bfb
The Hidden Cost of Modern JavaScript: Understanding let/const Performance | by Artem Khrienov | Medium
February 2, 2025 - In early 2023, the TypeScript team ... resulted in a significant performance improvement - approximately 10% faster parsing time in the TypeScript compiler itself....
🌐
Medium
dev-aditya.medium.com › which-is-better-const-vs-let-in-javascript-500eb3991f68
Which is better const vs let in Javascript | by Aditya Yadav | Medium
October 2, 2024 - While they share many similarities, they have key differences that affect how you use them in your code. However, when it comes to memory efficiency, there is no significant difference between const and let.
Top answer
1 of 6
185

My reply here is not javascript-specific.

As a rule of thumb in any language that lets me do so in a semi-easy way I'd say always use const/final/readonly/whatever it is called in your language whenever possible. The reason is simple, it's much easier to reason about code when it is dead obvious what can change and what cannot change. And in addition to this, in many languages you can get tool support that tells you that you are doing something wrong when you accidentially assign to a variable that you've declared as const.

Going back and changing a const to a let is dead simple. And going const by default makes you think twice before doing so. And this is in many cases a good thing.

How many bugs have you seen that involved variables changing unexpectedly? I'd guess a lot. I know that the majority of bugs that I see involve unexpected state changes. You won't get rid of all of these bugs by liberally using const, but you will get rid of a lot of them!

Also, many functional languages have immutable variables where all variables are const by default. Look at Erlang for example, or F#. Coding without assignment works perfectly in these languages and is one of the many reasons why people love functional programming. There is a lot to learn from these languages about managing state in order to become a better programmer.

And it all starts with being extremely liberal with const! ;) It's just two more characters to write compared to let, so go ahead and const all the things!

2 of 6
57

Be careful, because const object keys are mutable.

From here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

object keys are not protected

consider this example:

const colors = {red: "#f00"}; 
console.log(colors); // { "red": "#f00" }

colors.red = "#00f";
colors.green = "#0f0";
console.log(colors); // { "red": "#00f", "green": "#0f0" }

Same thing for arrays:

const numbers = [1, 2, 3];
console.log(numbers); // [ 1, 2, 3 ]

numbers.push(4);
console.log(numbers); // [ 1, 2, 3, 4 ]

I haven't decided totally myself, but I'm considering using const for all non-array/non-objects and use let for objects/arrays.

🌐
Esdiscuss
esdiscuss.org › topic › performance-concern-with-let-const
Performance concern with let/const
This feature of let/const is not the primary motivation for either feature (block scoped binding, inlinability and errors on re-assignment to const are the motivating features). The stated goal of 'let' is to replace 'var' in common usage (and if this is not the goal, we should not be adding 'let') Unless the above performance hit can be overcome, and given #2 above, let will slow down the web by ~5%.
Find elsewhere
🌐
MeasureThat
measurethat.net › Benchmarks › Show › 3873 › 0 › const-vs-let-vs-var
Benchmark: const vs let vs var - MeasureThat.net
var vs. const vs. let · var vs let vs const init · let vs const in tight loops · var vs const vs let · let vs const vs var · Comments · × · Do you really want to delete benchmark? Cancel Delete · × · FAQ: FAQ · Source code: GitHub/MeasureThat.net ·
🌐
O'Reilly
oreilly.com › library › view › learn-ecmascript › 9781788620062 › a9c74687-660d-4908-a44d-e9e0c3c4eeea.xhtml
Let versus var versus const performance benchmarks - Learn ECMAScript - Second Edition [Book]
February 26, 2018 - Clearly, performance-wise on Chrome, let on the global scope is slowest, while let inside a block is fastest, and so is const.
Authors   MEHUL MOHANNarayan Prusty
Published   2018
Pages   298
🌐
Reddit
reddit.com › r/learnjavascript › does let/const have a performance penalty? (compared to var)
r/learnjavascript on Reddit: Does let/const have a performance penalty? (compared to var)
April 23, 2025 -

For context, I decided to make a coding channel recently. I made a video explaining why var is discouraged and why let/const is a better alternative.

A commenter left this on my post, I've translated it into English:

Translate it yourself from my language! When using const or let, allocators and scopes will be checked every time where you use them.

This is not significant for variables < 10000, but more - you will waste seconds of time on this stupid concept with let and const. You either write the code correctly, quickly and efficiently, or don't bully people about the fact that it's better to use let or const.

Moreover, const is not a constant, go learn the base.

I researched this and came to differing conclusions.

I would love some feedback!

Thank you! 🙏

Note: I wasn't rude in my video (or bullying as the guys says it), I'm assuming he took it the wrong way.

🌐
Medium
medium.com › coding-at-dawn › are-let-and-const-faster-than-var-in-javascript-2d0b7f22a66
Are let and const faster than var in JavaScript? | by Dr. Derek Austin 🥳 | Coding at Dawn | Medium
January 6, 2023 - 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 …
🌐
Hacker News
news.ycombinator.com › item
Using const/let instead of var can make JavaScript code run 10× slower in Webkit | Hacker News
September 22, 2020 - As an aside, I’ve been seeing a number of articles show up that basically go like “Chrome implements x efficiently but semantically equivalent y poorly, you should use always use x. I really hope we don’t end up with JavaScript code being written to be optimized well on one particular ...
🌐
CodingNomads
codingnomads.com › javascript-let-vs-const
JavaScript: Prefer Const Over Let
Simplicity in Decision-Making: Using const as a default reduces the mental load of choosing between let and const. Avoiding Bugs: const reduces the risk of bugs in longer functions or closures since variables can't be reassigned. Understanding Mutation: Preferring const helps clarify the distinction between variable mutation and assignment, an important concept in JavaScript. Avoiding Meaningless Assignments: In certain scenarios, using const makes sense since the values should not be reassigned. Potential Performance Benefits: Some believe that const could lead to performance optimizations in JavaScript engines.
🌐
MeasureThat
measurethat.net › Benchmarks › Show › 18078 › 0 › const-vs-let-vs-var-fixed
Benchmark: const vs let vs var (fixed) - MeasureThat.net
var vs. const vs. let · var vs let vs const init · let vs const in tight loops · var vs const vs let · let vs const vs var · Comments · × · Do you really want to delete benchmark? Cancel Delete · × · FAQ: FAQ · Source code: GitHub/MeasureThat.net ·
🌐
Ponyfoo
ponyfoo.com › articles › var-let-const
Let's use `const`! Here's why. — Pony Foo
When const isn’t an option, because the variable needs to be reassigned later, we may resort to a let statement. Using let carries all the benefits of const, except that the variable can be reassigned.
🌐
Rust Programming Language
users.rust-lang.org › help
Const vs let in fn - help - The Rust Programming Language Forum
May 2, 2023 - If I have a fn that will run over and over again, comparing an input &str against an array of strings, which of the following options is more efficient, performance-wise? Does using let inside the fn body allocate or copy each time? Also, what about putting const HEADERS inside the fn body?
🌐
freeCodeCamp
freecodecamp.org › news › var-let-and-const-whats-the-difference
Var, Let, and Const – What's the Difference?
April 2, 2020 - While a const object cannot be updated, the properties of this objects can be updated. Therefore, if we declare a const object as this: ... This will update the value of greeting.message without returning errors. Just like let, const declarations are hoisted to the top but are not initialized.
Top answer
1 of 6
82

Basically,

  • use let if the variable's value will change during the code
  • use const if it won't and you / your team want to use const in those situations in the project you're working on; it's a matter of style

If you do use const, then it's surprising how often it turns out that the guidelines above mean you use const because you end up not needing to change a variable's value (if you're following the usual rules of keeping your functions of reasonable size and such). (Well, it surprised me, anyway...)

Using const when the variable's¹ value is not meant to change accomplishes a few things:

  1. It tells others reading your code that you don't intend the value to change.

  2. It gives you a nice proactive error if you change the code so it writes to that variable. (A decent IDE can flag this up proactively, but if not, you'll get the error when running the code.) You can then make an informed, intentional decision: Should you change it to let, or did you not mean to change that variable's value in the first place?

  3. It gives a hint to the JavaScript engine's optimizer that you won't be changing that variable's value. While the engine can frequently work that out through code analysis, using const saves it the trouble. (Caveat: I have no idea whether this is actually useful to the JavaScript engine. It seems like it would be, but runtime code optimization is a very complicated and sometimes non-intuitive process.)


¹ Yes, it's funny to use the term "variable" to refer to something that by definition doesn't vary. :-) The specification's term is "binding," but I bet you won't hear people talking about "bindings" in everyday conversation anytime soon... So the aggregate term will probably remain "variable" except when we can specifically refer to something as a "constant."

2 of 6
50

Using const by default is essentially a matter of programming style. However there are pros and cons to each:

Arguments in favor of using const by default

The arguments in favor of using const by default are the following:

  1. It avoids side effects caused by involuntary reassignments;
  2. During a code review, it removes an uncertainty, because the developer who sees a const variable can count on the certainty that it will not be reassigned;
  3. Maybe we could say it is more consistant with functional programming and immutable states.
  4. With TypeScript, there are some cases with better inferences.

Here is an example of advantage when using const with TypeScript:

const hello = "Hello" as string | undefined
if (hello !== undefined) {
    ["A", "B"].forEach(
        name => console.log(`${hello.toUpperCase()}, ${name}`) // OK
    )
}

With let, in strict mode, TypeScript detects an error:

let hello = // …
// …
        name => console.log(`${hello.toUpperCase()}, ${name}`)
//                             ^__ error here: Object is possibly 'undefined'.

Arguments in favor of using let by default

I summarize here the article Use “let” by default, not “const” that gives arguments in favor of using let by default rather than const:

  1. Re-assignment is not a dangerous thing, it is just... usual;
  2. If a variable could be reassigned, then it should be declared with let, because it is more expressive to reserve const for real constants;
  3. const is misleading because it doesn’t stop references being modified;
  4. It is two more characters to write and to bundle;
  5. Using const by default is inconsistant with function parameters;
  6. There is no performance gain to use const.