Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could someone help describe the two types of array storage in Javascript?

I'm reading this article about V8 on HTML5Rocks. The article is old but I understand almost none of it and it bothers me. I'm taking this 1 step at a time but could someone help me with the Arrays section?

The article states:

Arrays

In order to handle large and sparse arrays, there are two types of array storage internally:

  • Fast Elements: linear storage for compact key sets

  • Dictionary Elements: hash table storage otherwise

It's best not to cause the array storage to flip from one type to another.

Question:

What would a Fast Elements linear storage array look like?

What would a Dictionary Elements hash table array look like?

For prevention purposes, how would I "flip from one type to another"?

like image 886
Armeen Harwood Avatar asked Nov 22 '16 05:11

Armeen Harwood


People also ask

How arrays are stored in JavaScript?

In Javascript, an array is a Hashtable Object type so the interpreter doesn't need to keep track of physical memory and changing the value of an element doesn't affect other elements as they're not stored in a contiguous block of memory.

How many types of arrays are there in JavaScript?

Js array is divided into two types: One-dimensional arrays. Multi-dimensional arrays.

What is an array in JavaScript?

Arrays are used to store multiple values in a single variable. This is compared to a variable that can store only one value. Each item in an array has a number attached to it, called a numeric index, that allows you to access it. In JavaScript, arrays start at index zero and can be manipulated with various methods.

What is an array in JavaScript with example?

An array is an object that can store multiple values at once. For example, const words = ['hello', 'world', 'welcome']; Here, words is an array.


2 Answers

I will go a little other way round.

2) What would Dictionary Elements hash table Array look like?

A JavaScript object is a map from string to values. e.g.

var obj = {
  "name": "Sherlock Holmes",
  "address": "221B Baker Street"
}

V8 uses hash tables to represent objects unless using an optimized representation for special cases. This is much like a dictionary uses (words, meaning) pair.

Now, this hash table access is slow because initially all the keys and values in a hash table are undefined. On inserting a new pair, a hash is computed and the pair inserted at the insertion index. If there's already a key at that index, attempt to insert at next one and so on.

1) What would Fast Elements Linear storage Array look like?

In V8, an element is a property whose key is a non-negative integer (0, 1, 2, ...) i.e. a simple linear array whose properties can be accessed via a numerical index.

Fast elements are stored in a contiguous array. e.g.

var arr = [1, 2, 3];

They are a special case that is optimised for faster access as the index is already known and not to be computed.

3) For prevention purposes, How would I flip from one type to another?

For fast element, if you assign an index that's way past the end of the elements array, V8 may downgrade the elements to dictionary mode.

Reference: http://jayconrod.com/posts/52/a-tour-of-v8-object-representation

like image 134
Slartibartfast Avatar answered Oct 24 '22 11:10

Slartibartfast


sir i can be wrong but according to your question what i have observed is explained

when we initialise an array it internally gets key as 0,1,2....etc as i pushed element with value its into array but array does not consider it

ex :

var arr = new Array()

arr[0] = 1
arr[1] = 2
arr[2] = "myname";
arr['myname'] = nick;

but when i do arr.length i get 3 so it does not consider the key apart from numeric but if i write arr[3] = {myname:'nick'} then it consider it as elements.

internally i think to keep the linear array different it looks for '{}'

like image 40
Akshay Avatar answered Oct 24 '22 10:10

Akshay