When is an array, not an array?
Tags: javascript, computerscience
Published: 2018-11-20 13:31:13

---
title: When is an array, not an array?
published: true
tags: javascript, computerscience
canonical_url: https://www.codingblocks.net/programming/when-is-an-array-not-an-array/
---

<img src="https://thepracticaldev.s3.amazonaws.com/i/tpkhtxyqzz4agt1fywn1.jpg" alt="Solar Array Picture, Photo by Andreas Gücklhorn on Unsplash"/>

TL;DR: Don’t do weird stuff to JavaScript arrays.

\*ahem\*

## What are arrays?

In the [standard definition of Array data structures in Computer Science](https://en.wikipedia.org/wiki/Array_data_structure), arrays are a collection of like elements where any member may be accessed by an index. That definition doesn’t sound very different from an [Hash Table](https://en.wikipedia.org/wiki/Hash_table) or it’s [ilk](https://en.wikipedia.org/wiki/Associative_array), but the key distinctions here are that the elements of the array must take up the same size in memory, and that the array indexes need to be numeric so that the logical address of any individual element can be derived from that number.

> Arrays are typically created from contiguous memory. This makes for a simple calculation, like if we know that an Array lives at memory address 1024 and that each element in the array holds a 32-bit reference to an object then we can look up any individual item with a simple computation:  
> element\_address = (1024 + (32 \* index))
> 
> You want the element at index 14? Here ya go, it’s the 32bits starting at address 1472!

## What makes arrays important?

It’s important that indexes be numeric because it means we can _compute_ the memory address of any element in the array in [constant time](https://en.wikipedia.org/wiki/Time_complexity#Constant_time). This makes for the fastest [random access](https://en.wikipedia.org/wiki/Random-access_memory) of any [data structure](https://en.wikipedia.org/wiki/Data_structure) – even in the worst case scenario. Hash Tables are great and all, but there is overhead associated with their [hashing and collision handling](http://interactivepython.org/runestone/static/pythonds/SortSearch/Hashing.html).

Check out the [Big-O Algorithm Complexity Cheat Sheet](http://bigocheatsheet.com/) for a nice visual comparison.

So, back to our original question…

## When is an array, not an array?

JavaScript is notoriously [loosey goosey](https://blog.mgechev.com/2013/02/22/javascript-the-weird-parts/), and it’s array types are no exception.

Sure, we can do all the normal array type operations. But we can also do _weird_ stuff.

Here’s a simple array definition, but how much memory do you think is ultimately allocated by this operation?

```
var myArray = [0,1,2];
```

Similar code in a language like C would have allocated an array with 3 x sizeof(int). Trying to set a value outside of that memory block (say at [100000]) would go badly in most languages, but JavaScript doesn’t mind at all. In fact, JavaScript is extremely lenient in what it will take. Let’s continue the example…

```
myArray[100000] = 100000; // JS is cool with this
myArray[-1] = -1.33333; // and this
myArray['mmm...need more coffee'] = 'go get it, lazy bones!'; // this too
```

The above examples show several crimes against nature. Exceeding the bounds of original declaration, negative indexes, non-numeric indexes, setting different value types…heinous crimes indeed. These operations are incompatible with the definition above.

Given the information above about the key defining features of array data structures…how can JavaScript Arrays possibly be arrays? The answer, my friend, is ~blowing in the wind~ that there is a difference between [array data structures](https://en.wikipedia.org/wiki/Array_data_structure) and [array types](https://en.wikipedia.org/wiki/Array_data_type).

> JavaScript arrays are always array types, but they aren’t always array data structures.

JavaScript doesn’t have _an_ implementation, it has many. [V8, TraceMonkey, and Chakra](https://en.wikipedia.org/wiki/JavaScript_engine) are all examples of popular JavaScript engines. This makes it hard to really say what “JavaScript does” when it comes to technical implementations. Even if we managed to find the relevant spot(s) in code for a particular engine, it could all change tomorrow. That said, things are pretty stable so a radical re-working of array implementations in V8 (for example) are unlikely. Code diving is difficult given the size of the engines, but lucky for us some [really smart people have opted to guide us through the most remarkable bits](https://v8.dev/blog/elements-kinds).

In [V8](https://github.com/v8/v8/tree/master/src/base) (and most other implementations) The underlying data structures used to represent your JavaScript array in memory can change based on your usage. If you start out with an array of integers, then V8 will represent that with a [dynamic array](https://en.wikipedia.org/wiki/Dynamic_array) of integers. If you exceed the bounds of your array, then V8 will either allocate you a new array that is big enough to fit your new indexes or in some cases might even **dynamically convert your array to a new data structure** that is more efficient for [dealing with sparse values](https://en.wikipedia.org/wiki/Sparse_matrix). If you add a value to your array that does not match with the data type that V8 started you with then it will allocate you a new array with a more general type. In the code above we started with integers, but the type was changed once we added a decimal, and would have changed again when we added the string except that the index I used was also a string so once again, V8 would have **dynamically converted the underlying data structure** to accommodate.

> The underlying data structures used to represent your JavaScript array in memory can change based on your usage.

How does JavaScript get away with calling their arrays…arrays? Formal definitions don’t always line up cleanly with the “real” world and there is a distinction between Array Data Structures and Array Types. Array types are sometimes implemented with other data structures, like in the cases listed above, and that is okay. JavaScript is not the only language that is loosey goosey, at least it’s indexes [start with zero](http://xahlee.info/comp/comp_lang_array_index_start_0_or_1.html)!

## So, what have we learned?

Don’t do weird stuff to JavaScript arrays. JS is lenient and complicated, and there are performance implications in addition to the normal readability nightmares caused by the examples shown above. [Read this article, and watch the video if you want to learn more.](https://v8.dev/blog/elements-kinds)

Thanks for reading, let me know what you think in the comments!

* * *

Like this post? It was a result of research for [this podcast episode on arrays and other similar data structures](https://www.codingblocks.net/podcast/data-structures-arrays-and-array-ish/), so subscribe to [Coding Blocks](https://itunes.apple.com/us/podcast/coding-blocks/id769189585?mt=2) if you are interested in topics like this.

Photo by <a href="https://unsplash.com/photos/Ilpf2eUPpUE?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Andreas Gücklhorn</a> on <a href="/search/photos/array?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>