Understanding JavaScript Arrays: Continuous vs. Holey Arrays

Rishabh RawatRishabh Rawat
3 min read

JavaScript, a cornerstone of web development, operates on the powerful V8 engine. Within this realm, another engine, the V8 debugger, plays a pivotal role, especially when it comes to debugging and understanding how JavaScript processes data.

Array Basics

At the heart of JavaScript's data structures lies the array. But not all arrays are created equal. Depending on their structure, arrays can be categorized into two Types: Continuous and Holey.

Continuous Arrays

A continuous array is one without any gaps or holes. For instance, an array [1, 2, 3, 4] is continuous. Every element is followed by the next in sequence.

Holey Arrays

In contrast, holey arrays have gaps or holes between their elements. Consider an array [1, 2, , 4]. Here, the third index is skipped, creating a hole in the array.

Performance Implications

The way an array is structured can impact the performance of a JavaScript program. Continuous arrays are generally more straightforward for the engine to process, while holey arrays require more intricate handling.

  1. Data Types in Continuous Arrays:

    • Packed SMI (Small Integer) elements: Arrays like [1, 2, 3, 4] fall into this category.

    • Packed Double elements: When double-precision floating-point numbers are introduced [1, 2, 3, 4, 5.69].

    • Packed elements: General packed elements that include mixed data types[1, 2, 3, 4 , 5.69 , '6' ].

  2. Data Types in Holey Arrays:

    • Holey SMI elements: Arrays with holes containing small integers.

    • Holey Double elements: Arrays with holes containing double-precision floating-point numbers.

    • Holey Packed elements: Arrays with mixed data types and holes.

The Cost of Holes

Initializing and accessing elements in holey arrays can be more expensive. When a hole exists, JavaScript's engine must traverse multiple paths to retrieve or set an element, checking prototype chains and object properties. This added complexity impacts performance.

  1. Bound Check: The engine first checks if the index exists within the bounds of the array.

  2. hasOwnProperty(arr): Next, it checks if the array itself has a direct property at that index.

  3. hasOwnProperty(arr.prototype): If not found, the engine then inspects the array's prototype to see if it contains the desired property.

  4. hasOwnProperty(object.prototype): As a final step, if the prototype doesn't have the property, it checks the object's prototype chain.

Array Transformation

Once an array is initialized as holey, transforming it later by removing holes doesn't change its nature. This means that once downgraded to a holey array, you cannot revert to a continuous array, affecting performance.

Performance Hierarchy

To summarize, in terms of performance, the hierarchy stands as:

  • Continuous Arrays: Packed SMI > Packed Double > Packed

  • Holey Arrays: Holey SMI > Holey Double > Holey Packed

In summary, understanding the nuances between continuous and holey arrays can offer insights into optimizing JavaScript code for better performance.

Hitesh Choudhary

10
Subscribe to my newsletter

Read articles from Rishabh Rawat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rishabh Rawat
Rishabh Rawat