2

While I don't know the name of this functionality:

a = []
a[57] = "test";

console.log(a.length)
console.log(a[57])
console.log(a)

I am in awe that this is possible. Coming from a C background, this behavior is very different than anything like allocating memory. So a few questions:

  1. Is Javascript allocating memory for the first 57 "empty" entries in the array?
  2. If not, how does Javascript keep O(1) for lookups using fragmented memory addresses?
  3. Is a Javascript Array really an Array or just an Object with integers as keys?
  4. What is this design pattern called so that I can research it further?
Calvin Nunes
  • 6,376
  • 4
  • 20
  • 48
toobulkeh
  • 1,618
  • 1
  • 14
  • 22
  • 2
    `C` is a very low level language, `JS` on the other hand is not. It's like a "*do whatever you please*" language and we'll figure out how to do the backend of things. Bear in mind that assigning a variable to the value of another variable you copy the reference and not the value. – Xorifelse Sep 05 '18 at 18:05
  • 2
    an array is an *exotic object* according to the standard ecma 262. – Nina Scholz Sep 05 '18 at 18:05
  • 3
    Good read: https://stackoverflow.com/questions/4524067/if-i-set-only-a-high-index-in-an-array-does-it-waste-memory – Calvin Nunes Sep 05 '18 at 18:10
  • Also: https://stackoverflow.com/questions/5048371/are-javascript-arrays-primitives-strings-objects – Calvin Nunes Sep 05 '18 at 18:12
  • This is possible in just about every dynamic language, and even in C++ with vectors. – user229044 Sep 05 '18 at 18:45
  • 1
    @Xorifelse "Bear in mind that assigning a variable to the value of another variable you copy the reference and not the value." This only applies to non-primitives as I understand it, not booleans, numbers and strings. – Dexygen Sep 06 '18 at 00:36
  • @GeorgeJempty This is true, obviously. You primarily encounter this within objects, assigning a variable to value to a property and then the mutating begins.. A good way to counter that is by using [pure functions](https://hackernoon.com/javascript-and-functional-programming-pt-3-pure-functions-d572bb52e21c). – Xorifelse Sep 08 '18 at 09:11

1 Answers1

4
  1. Is Javascript allocating memory for the first 57 "empty" entries in the array?

1. No

JS Arrays may or may not be dense based on the engine, how you construct them, and how you use them afterwards.

Popular engines will create dense arrays whenever you construct them with all its elements defined, and no holes.

Dense array definition:

[0,1,2,3,4];

Array.apply(null, Array(4)); // This is creating an Array expanding the internal Sparse Array as it initial values.

Sparse Array Definition:

[0,,2,3,4];

new Array();

new Array(4);
  1. If not, how does Javascript keep O(1) for lookups using fragmented memory addresses?

2. Depends on the Array

For dense arrays it will store everything inside the same block of memory.

For sparse arrays it will create a hash table and store pointers to each of the elements independently.

  1. Is a Javascript Array really an Array or just an Object with integers as keys?

3. They are a type of Object

Arrays are regular Objects, with some properties that let them behave as you would expect.

Accessing elements with integer keys such as 1 (or strings that represent integers such as "1") will retrieve whatever is store under that key. Using non-integers (nor string representations of integers) will still set/retrieve Object properties.

Array length is just an attribute of the Object and its value it's not affected by modifying Object properties.

  1. What is this design pattern called so that I can research it further?

4. Not sure

Roberto Decurnex
  • 2,514
  • 1
  • 19
  • 28