6

I always thought that object in javascript are simple key-value pair i.e. hash tables, but after reading this article I am confused.

The author specially says:

when you use an object as if it was a hash table, it will be turned into a hash table.

So, what the hell was it before? specially the whole para got me confused. Can somebody shed some light on?

xaxxon
  • 19,189
  • 5
  • 50
  • 80
CodeYogi
  • 1,352
  • 1
  • 18
  • 41
  • This is probably not meant on a programming language level. But on how you use the language. – Pascal Apr 29 '16 at 13:58
  • 2
    no time for a full answer, but the JIT can turn some objects into code where properties are looked up _very_ efficiently almost like C structures and it's O(1). Except when it can't, when it uses a real hash table as the backing store instead. – Alnitak Apr 29 '16 at 14:00

1 Answers1

10

According to this answer, in V8 there are two modes an object can have—Dictionary mode and Fast mode.

Objects are originally in fast mode where no hash map—no computation—is required for property access. It stores objects like structs in C. It's only once you start using an "object as if it was a hash table, it will be turned into a hash table"—dictionary mode. Once this happens, you get the performance penalty of a hash map being used behind the scenes for property access.

For example:

// running node with `--allow-natives-syntax` flag

var obj = { a: true, b: false };
%HasFastProperties(obj); // true (Fast mode)
delete obj.a;
%HasFastProperties(obj); // false (Dictionary mode)

Or:

var obj = { a: true, b: false };
%HasFastProperties(obj); // true (Fast mode)
// add lots of properties
for (var i = 0; i < 100; i++) {
    obj["prop" + i] = i;
}
%HasFastProperties(obj); // false (Dictionary mode)

The reason it goes into dictionary mode when doing this is a performance optimization. It's faster to add/remove properties in dictionary mode than fast mode and so the V8 engine optimizes for changing structure instead of property access when it detects this behaviour (Read more here).

Community
  • 1
  • 1
David Sherret
  • 101,669
  • 28
  • 188
  • 178
  • Is this specific to some JavaScript engine? – svick Apr 29 '16 at 15:07
  • @svick yeah, only answering for the engine the article in the question was talking about—v8. I can't say for sure about other engines because they could handle this differently behind the scenes. – David Sherret Apr 29 '16 at 15:10
  • A good article, it states that what is the difference between the two, my question is if the Fast mode is so optimized then why do we need the dictionary aka hash table mode at all? – CodeYogi Apr 29 '16 at 15:10
  • @CodeYogi ok, I didn't see that question in your question ;) I can't source the reason for that, but I would assume that if an object is being used a lot like a dictionary then it makes sense to optimize it for repeated adding and deleting of properties by putting it in dictionary mode. – David Sherret Apr 29 '16 at 15:14
  • @CodeYogi here's another question that talks about [the pros and cons of dictionary mode](http://stackoverflow.com/questions/23455678/pros-and-cons-of-dictionary-mode) – David Sherret Apr 29 '16 at 15:29
  • I am curious about why dictionary mode at all, if we can have fast mode? – CodeYogi Apr 29 '16 at 15:30
  • @CodeYogi "[Dictionary mode] makes things slower, yes, but it would be even slower if V8 would try to keep the object in fast mode even when you are changing its shape all the time." - [See here](http://stackoverflow.com/a/23491526/188246) – David Sherret Apr 29 '16 at 16:06
  • @CodeYogi, just to add to the confusion: the fast vs dict mode distinction is still a vast simplification. In reality, there are more than half a dozen different ways in which properties can be represented in V8, and the VM uses runtime profiling and various heuristics to dynamically change between them. JavaScript is such a well-designed language that you cannot make it fast without jumping through a gazillion hoops like that (and sometimes, not even then). – Andreas Rossberg May 01 '16 at 08:44