WhatIsJavaScript
WhatIsJavaScript

Reputation: 31

Does there exist an array/list in JavaScript under the hood?

I want to understand if there exists a list/Array in JavaScript as opposed to an Array like Object. If there are some resources or info on this I would love that.

So to my understanding, arrays in JavaScript are just objects with the index of the array being a key in the object. But objects are hash maps correct? In which case hash maps takes a key, hashes it to a index and then stores that value at the index in a list/array. This way if we ever need to access a key in the object, we can easy hash the key and go to the array to access the value in constant time. But arrays don't really exist in JavaScript, so is there some array/list like data structure under the hood in JavaScript that we just don't have access to?

Upvotes: 1

Views: 566

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138457

JavaScript is an interpreted language. You could write a valid JS engine in many different languages, and thus the specification itself provides a very abstract memory model. That means that objects are A collection of key-value pairs that provide ways to access these pairs, and arrays are exotic objects, that treat numeric keys differently. That's it, the specification doesn't define anything else, thus how this is actually represented in the memory is totally up to the engine.

You are right though that it really makes sense to implement objects as hashtables internally (in some situations), and arrays as real arrays or lists. Most engines use different underlying implementations depending on the way you use the objects / arrays though.

Worth reading:

Element kinds in v8

Fast properties in v8

Upvotes: 5

Related Questions