<story><title>V8 has optimized new JavaScript language features (2018)</title><url>https://github.com/thlorenz/v8-perf/blob/master/language-features.md</url></story><parent_chain><item><author>chrismorgan</author><text>&amp;gt; &lt;i&gt;const performs a lot better in optimized code than var or let&lt;/i&gt;&lt;p&gt;This puzzles me; if only ever one value is assigned, I would have expected at least &lt;i&gt;let&lt;/i&gt; to perform identically to &lt;i&gt;const&lt;/i&gt; in optimised code, because I expect the optimiser to look at the &lt;i&gt;let&lt;/i&gt; and say “never reassigned, turn it into a &lt;i&gt;const&lt;/i&gt;”. By the sound of it, I’m wrong, and I’d be interested to know &lt;i&gt;why&lt;/i&gt; I’m wrong.</text></item></parent_chain><comment><author>rewq4321</author><text>JavaScript is very dynamic. Here I define a variable with `let` and then change it:&lt;p&gt;let foo = 10;
eval(&amp;quot;fo&amp;quot;+&amp;quot;o = 10&amp;quot;);&lt;p&gt;I could &amp;quot;obfuscate&amp;quot; that eval assignment as much as I like. So you can&amp;#x27;t completely statically analyse `let` variables.&lt;p&gt;That said, it&amp;#x27;s likely you&amp;#x27;d end up with perf &lt;i&gt;very&lt;/i&gt; close to `const` in a very &amp;quot;hot&amp;quot; part of your code, since a good JIT compiler like V8 will eventually make &amp;quot;assumptions&amp;quot; about your code, and optimise around them, while having (ideally cheap) checks in place to ensure the assumptions continue to hold.</text></comment>
<story><title>V8 has optimized new JavaScript language features (2018)</title><url>https://github.com/thlorenz/v8-perf/blob/master/language-features.md</url></story><parent_chain><item><author>chrismorgan</author><text>&amp;gt; &lt;i&gt;const performs a lot better in optimized code than var or let&lt;/i&gt;&lt;p&gt;This puzzles me; if only ever one value is assigned, I would have expected at least &lt;i&gt;let&lt;/i&gt; to perform identically to &lt;i&gt;const&lt;/i&gt; in optimised code, because I expect the optimiser to look at the &lt;i&gt;let&lt;/i&gt; and say “never reassigned, turn it into a &lt;i&gt;const&lt;/i&gt;”. By the sound of it, I’m wrong, and I’d be interested to know &lt;i&gt;why&lt;/i&gt; I’m wrong.</text></item></parent_chain><comment><author>dean177</author><text>The const keyword also guarantees that once a value is assigned to its slot it won&amp;#x27;t change in the future.
As a result TurboFan skips loading and checking const slot values slots each time they are accessed (Function Context Specialization)&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;thlorenz&amp;#x2F;v8-perf&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;language-features.md#const&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;thlorenz&amp;#x2F;v8-perf&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;language-fea...&lt;/a&gt;</text></comment>