0

JavaScript objects are easy to use as hashmaps as they are essentially just a collection of key/value pairs. I am concerned with the memory usage and the time cost of storage and retrieval. I guess that the answer to this differs with the implementations of JavaScript engines. MDN links to the Wikipedia hashmap article but what I don't know is how big the hash table is for an object.

So what I want to know is, how expensive are objects in memory and how often is storage/retrieval an O(1) operation and how often is it an O(n) operation?

Jack Allan
  • 14,554
  • 11
  • 45
  • 57

1 Answers1

0

I think this depends on the particular JavaScript engine that you're using.

V8, for instance, in the simplest case translates a hashmap to an array and stores it a single block of memory.

Why?

Because it's faster to access an array element given an offset than a hashtable for obvious reasons.

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

I hope this answers your question at least partially :)

andrusieczko
  • 2,824
  • 12
  • 23