<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Web dev stuff]]></title><description><![CDATA[Web dev stuff]]></description><link>https://webdevwithmasterv.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 21 Nov 2026 16:49:22 GMT</lastBuildDate><atom:link href="https://webdevwithmasterv.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[I Thought JavaScript Arrays Were Simple. Then I Learned How V8 Optimizes Them.]]></title><description><![CDATA[Learn how Google's V8 engine optimizes JavaScript arrays using packed and holey elements, elements kind transitions, and smarter array creation.]]></description><link>https://webdevwithmasterv.hashnode.dev/i-thought-javascript-arrays-were-simple-then-i-learned-how-v8-optimizes-them</link><guid isPermaLink="true">https://webdevwithmasterv.hashnode.dev/i-thought-javascript-arrays-were-simple-then-i-learned-how-v8-optimizes-them</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[V8]]></category><category><![CDATA[v8 debugger engine]]></category><category><![CDATA[performance]]></category><category><![CDATA[Array in JavaScript]]></category><dc:creator><![CDATA[Vraj Vyas]]></dc:creator><pubDate>Wed, 18 Nov 2026 22:18:21 GMT</pubDate><content:encoded><![CDATA[<p>When working with arrays, we often think about optimizing the operations we perform on them, such as searching, sorting, and inserting elements. But did you know that the V8 engine has its own way of optimizing arrays internally?</p>
<p>Understanding how it works can help us write JavaScript code that gives the engine better opportunities to optimize, starting right from how we create and modify our arrays.</p>
<h2>How Does V8 Look at Arrays?</h2>
<p>V8 internally categorizes arrays using something called <strong>Elements Kinds</strong>.</p>
<p>These elements kinds help V8 determine how an array's elements are stored and how operations on that array can be optimized.</p>
<p>Let's start with the two main characteristics V8 considers when dealing with common array elements kinds.</p>
<h3>1. PACKED vs HOLEY Arrays</h3>
<p>Based on whether an array has missing indices, V8 categorizes arrays into two groups:</p>
<ul>
<li><strong>PACKED:</strong> An array where every index has an assigned value. In other words, there are no missing indices.</li>
<li><strong>HOLEY:</strong> An array that contains missing indices, also known as holes.</li>
</ul>
<p>For example:</p>
<pre><code class="language-javascript">// PACKED array
const packed = [1, 2, 3, 4];

// HOLEY array
const holey = [1, 2, , 4];
</code></pre>
<p>The first array has a value at every index, whereas the second array has a missing value at index 2.</p>
<p>But that's not all. V8 further categorizes these arrays according to the type of values they contain.</p>
<h3>2. SMI, DOUBLE and ELEMENTS</h3>
<p>There are three common value representations:</p>
<ol>
<li><strong>SMI (Small Integer):</strong> Used for small integer values that can be represented using V8's Smi format.</li>
<li><strong>DOUBLE:</strong> Used for floating-point numbers and numbers that cannot be represented as Smis.</li>
<li><strong>ELEMENTS:</strong> Used for general JavaScript values that cannot be represented as Smis or doubles, such as strings, objects and other values.</li>
</ol>
<p>Let's look at some examples:</p>
<pre><code class="language-javascript">// PACKED_SMI_ELEMENTS
const arr1 = [1, 2, 3, 4];

// PACKED_DOUBLE_ELEMENTS
const arr2 = [1, 2.5, 3];

// PACKED_ELEMENTS
const arr3 = [1, "hello", true];

// HOLEY_SMI_ELEMENTS
const arr4 = [1, , 3];
</code></pre>
<p>Notice that <code>PACKED</code> and <code>HOLEY</code> describe the structure of an array, whereas <code>SMI</code>, <code>DOUBLE</code>, and <code>ELEMENTS</code> describe how V8 represents its values.</p>
<p>Combining these gives us six common elements kinds:</p>
<table>
<thead>
<tr>
<th>Packed</th>
<th>Holey</th>
</tr>
</thead>
<tbody><tr>
<td>PACKED_SMI_ELEMENTS</td>
<td>HOLEY_SMI_ELEMENTS</td>
</tr>
<tr>
<td>PACKED_DOUBLE_ELEMENTS</td>
<td>HOLEY_DOUBLE_ELEMENTS</td>
</tr>
<tr>
<td>PACKED_ELEMENTS</td>
<td>HOLEY_ELEMENTS</td>
</tr>
</tbody></table>
<p>These are the common fast elements kinds, not every elements kind supported by V8.</p>
<h2>Understanding Elements Kind Transitions</h2>
<p>Here's where things get interesting.</p>
<p>V8 doesn't just assign an elements kind when an array is created. The elements kind can also change as we add or modify elements.</p>
<p>Consider this example:</p>
<pre><code class="language-javascript">const numbers = [1, 2, 3];

// PACKED_SMI_ELEMENTS

numbers.push(4.5);

// PACKED_DOUBLE_ELEMENTS

numbers.push("hello");

// PACKED_ELEMENTS
</code></pre>
<p>Initially, the array contains only small integers, so V8 can use <code>PACKED_SMI_ELEMENTS</code>.</p>
<p>When we add a floating-point number, it transitions to <code>PACKED_DOUBLE_ELEMENTS</code>.</p>
<p>When we add a string, it transitions again to <code>PACKED_ELEMENTS</code>.</p>
<p>This happens because V8 needs a more general representation to accommodate the new values.</p>
<h3>What About NaN and Infinity?</h3>
<p>Here's something interesting. Even though <code>NaN</code> and <code>Infinity</code> are both of type <code>number</code> in JavaScript, V8 represents them as doubles.</p>
<p>This means adding either of them to an array containing only small integers causes an elements kind transition from <code>PACKED_SMI_ELEMENTS</code> to <code>PACKED_DOUBLE_ELEMENTS</code>.</p>
<p>Let's see:</p>
<p>```javascript const numbers = [3, 2, 1];</p>
<p>// PACKED_SMI_ELEMENTS</p>
<p>numbers.push(NaN, Infinity);</p>
<p>// PACKED_DOUBLE_ELEMENTS ```</p>
<p>So, even a single <code>NaN</code> or <code>Infinity</code> is enough to change the elements kind of the entire array.</p>
<p>If you're working with arrays that are intended to contain only integers and perform a lot of operations on them, consider validating or normalizing these values before adding them.</p>
<p>However, don't remove valid values just to chase a potentially insignificant performance improvement.</p>
<h3>Can an array go back to its previous elements kind?</h3>
<p>Generally, no.</p>
<p>These transitions move from more specific representations to more general ones.</p>
<p>For example, replacing the floating-point number with an integer later doesn't automatically turn a <code>PACKED_DOUBLE_ELEMENTS</code> array back into <code>PACKED_SMI_ELEMENTS</code>.</p>
<p>Similarly, once an array becomes holey, simply filling its missing indices doesn't generally restore its original packed kind.</p>
<p>There is a newer exception involving <code>Array.prototype.fill</code>, which V8 documented in an update dated February 28, 2025. So this rule shouldn't be treated as an absolute for every array operation.</p>
<h2>Why Are HOLEY Arrays Less Efficient?</h2>
<p>Let's take an example:</p>
<pre><code class="language-javascript">const arrOne = [1, 2, 3, 4];

arrOne[7] = 7;

console.log(arrOne);
// [1, 2, 3, 4, &lt;3 empty items&gt;, 7]

console.log(arrOne.length);
// 8

console.log(arrOne[4]);
// undefined
</code></pre>
<p>When we assign a value at index 7, JavaScript automatically increases the array's length to 8.</p>
<p>But what happens to indices 4, 5 and 6?</p>
<p>They become empty slots, also known as holes.</p>
<p>Now you might wonder why accessing <code>arrOne[4]</code> is different from accessing an element in a packed array.</p>
<p>Let's understand.</p>
<p>When JavaScript accesses an array index, it must follow normal property lookup semantics.</p>
<p>In the case of a hole, the array doesn't actually have its own property at that index. The engine must account for the possibility that the missing property exists somewhere in the prototype chain.</p>
<p>This means additional checks and potentially a lookup through the prototype chain are required.</p>
<p>It isn't that JavaScript literally calls <code>hasOwnProperty()</code> every time you access a hole. Rather, the engine must preserve the same property lookup behaviour that JavaScript specifies.</p>
<p>We can see the difference between a hole and an actual <code>undefined</code> value:</p>
<pre><code class="language-javascript">const holeyArray = [1, , 3];

console.log(holeyArray[1]);
// undefined

console.log(1 in holeyArray);
// false

const normalArray = [1, undefined, 3];

console.log(normalArray[1]);
// undefined

console.log(1 in normalArray);
// true
</code></pre>
<p>Both array accesses return <code>undefined</code>, but internally they are not the same.</p>
<p>The first array has a missing index, while the second has an actual element whose value is <code>undefined</code>.</p>
<p>This is one reason V8 can optimize packed arrays more aggressively than holey arrays.</p>
<p><strong>Holes require additional work. That's why avoiding unnecessary holes can help V8 optimize array operations.</strong></p>
<h2>A Common Mistake When Creating Arrays</h2>
<p>One common mistake is creating an array using the <code>Array()</code> constructor with a length:</p>
<pre><code class="language-javascript">const arrTwo = new Array(3);
</code></pre>
<p>This creates an array with three empty slots, not an array containing three <code>undefined</code> values.</p>
<p>In V8, this commonly starts as <code>HOLEY_SMI_ELEMENTS</code>.</p>
<p>Even if we fill all its positions using direct assignments:</p>
<pre><code class="language-javascript">arrTwo[0] = 10;
arrTwo[1] = 20;
arrTwo[2] = 30;
</code></pre>
<p>The array generally remains holey.</p>
<p>So, what should we do instead?</p>
<h3>When we already know the values</h3>
<p>Use an array literal:</p>
<pre><code class="language-javascript">const arr = [10, 20, 30];
</code></pre>
<h3>When we want to add values dynamically</h3>
<p>Start with an empty array and use <code>push()</code>:</p>
<pre><code class="language-javascript">const arr = [];

arr.push(10);
arr.push(20);
arr.push(30);
</code></pre>
<p>This allows us to add elements sequentially without creating unnecessary holes.</p>
<h2>What About for...of and forEach?</h2>
<p>Another interesting point from V8's performance recommendations is array iteration.</p>
<p>We often use traditional <code>for</code> loops, <code>for...of</code>, and methods like <code>forEach()</code>.</p>
<p>V8's documentation explains that <code>for...of</code> and <code>forEach()</code> can perform on par with traditional <code>for</code> loops in its tested context.</p>
<p>However, there's an important difference in how these methods handle holes.</p>
<pre><code class="language-javascript">const arr = [10, , 30];

// for...of
for (const value of arr) {
    console.log(value);
}

// Output:
// 10
// undefined
// 30
</code></pre>
<p>Compare that with <code>forEach()</code>:</p>
<pre><code class="language-javascript">arr.forEach((value) =&gt; {
    console.log(value);
});

// Output:
// 10
// 30
</code></pre>
<p><code>forEach()</code> skips empty slots, whereas <code>for...of</code> visits the positions and yields <code>undefined</code> for a hole (assuming no inherited indexed property).</p>
<p>So, don't blindly choose one iteration method because someone says it's faster. Understand the behaviour you need and benchmark performance when it actually matters.</p>
<p>One more thing: always be careful with loop boundaries.</p>
<pre><code class="language-javascript">// Correct
for (let i = 0; i &lt; arr.length; i++) {
    console.log(arr[i]);
}

// Avoid accidentally reading beyond the array's length
for (let i = 0; i &lt;= arr.length; i++) {
    console.log(arr[i]);
}
</code></pre>
<p>The second loop accesses one index beyond the array's length. V8 specifically warns that unnecessary out-of-bounds array reads can affect optimization.</p>
<h2>Can We Actually See These Elements Kinds?</h2>
<p>Yes! And this is probably one of the most interesting parts of the video.</p>
<p>V8 provides internal debugging functionality that allows us to inspect the elements kind of an array.</p>
<p>For this, we can use a V8 debug build and run it through <code>d8</code> with the <code>--allow-natives-syntax</code> flag.</p>
<p>We can then use:</p>
<pre><code class="language-javascript">const arr = [1, 2, 3];

%DebugPrint(arr);
</code></pre>
<p>This displays internal information about the array, including its elements kind.</p>
<p>For example, you might see something like:</p>
<pre><code class="language-text">elements = ... &lt;FixedArray[3]&gt; [PACKED_SMI_ELEMENTS]
</code></pre>
<p>One important thing to remember: <code>%DebugPrint()</code> is a V8 internal debugging function. It isn't standard JavaScript and won't normally work in your browser console or regular Node.js execution.</p>
<p>You can explore <a href="https://github.com/GoogleChromeLabs/jsvu">JSVU (JavaScript Version Updater)</a> to get access to V8 debug builds and experiment with these internals.</p>
<h2>What Did I Learn?</h2>
<p>Before watching this video, I never really thought about how JavaScript engines internally handle arrays.</p>
<p>We generally focus on the operations we perform, like searching, sorting and inserting elements, but there's another side to performance that happens underneath our code.</p>
<p>Learning about elements kinds gave me a better understanding of why the way we create and modify arrays can matter to the JavaScript engine.</p>
<p>Some takeaways:</p>
<ul>
<li>Prefer array literals when the values are already known.</li>
<li>Use an empty array with <code>push()</code> when adding elements dynamically.</li>
<li>Avoid creating unnecessary holes.</li>
<li>Understand that V8 can transition arrays to more general elements kinds.</li>
<li>Don't blindly optimize every array. Measure performance before making changes.</li>
</ul>
<p>One last thing: these are V8-specific implementation details, not rules that every JavaScript engine must follow. Also, the actual performance difference is often negligible in everyday applications.</p>
<p>Sometimes, understanding what happens behind the scenes is just as interesting as learning how to write the code itself.</p>
<h2>References and Further Reading</h2>
<ul>
<li><a href="https://v8.dev/blog/elements-kinds">Elements kinds in V8 – Official V8 Blog</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections">JavaScript Arrays – MDN</a> (When I wrote this, it didn't cover much of the topics mentioned above, so you can mainly use it as a reference for <strong>array</strong> itself.)</li>
<li><a href="https://github.com/GoogleChromeLabs/jsvu">JSVU – JavaScript Version Updater</a></li>
</ul>
<h2>The Video That Inspired This Article</h2>
<p>This article is based on the final video of the JavaScript series by Chai aur Code.</p>
<p><a class="embed-card" href="https://youtu.be/ZRS485LxX0s?si=xKkf3iUiJHgys_n0">https://youtu.be/ZRS485LxX0s?si=xKkf3iUiJHgys_n0</a></p>
]]></content:encoded></item></channel></rss>