Difference between an object and a hash?

JavascriptJson

Javascript Problem Overview


In JavaScript, what is the difference between an object and a hash? How do you create one vs the other, and why would you care? Is there a difference between the following code examples?

var kid = {
 name: "juni",
 age: 1
}

And:

var kid = new Object();
kid.name = "juni";
kid.age = 1;

And:

var kid = new Object();
kid["name"] = "juni";
kid["age"] = 1;

Can you think of any other code example I should illustrate?

The core question here is what is the difference between an object and a hash?

Javascript Solutions


Solution 1 - Javascript

There just isn't any. All three of those are literally equal.

Solution 2 - Javascript

They are different notation systems that you can use interchangeably. There are many situations where using the bracket syntax [ ] can be more appealing, an example would be when referencing an object with a variable.

var temp  = "kid";
var obj = new Object();
obj[temp] = 5; // this is legal, and is equivalent to object.kid
obj.temp = 5; // this references literally, object.temp

Solution 3 - Javascript

In other languages such as Java and C# it's possible to use any object (not just a string or a number) as a key in a hash table/hash map, which is not the case in JavaScript: keys are simply converted to strings.

var h = {}, k = {};
h[k] = "One";
alert( h[ "[object Object]" ] ); // Alerts "One"

It can be useful to use arbitrary objects as keys, in which case you can use something like jshashtable.

Disclaimer: I wrote jshashtable.

Solution 4 - Javascript

Actually, every object in JavaScript IS a hash. This is a hash of object's properties and methods. In fact, everything in Javascript is a hash (i.e a list of name/value pairs).

Every time you call object's method, property, or just reference any variable, you perform internal hash lookup.

Solution 5 - Javascript

There isn't any difference in any of your samples. They are all objects with named properties. You've just shown different ways of creating/referencing those properties.

Solution 6 - Javascript

They are the same.

you can use them interchangeably.

Solution 7 - Javascript

I think this is all the same. The third version could used with dynamic property names. The first one is the shortest to write.

Solution 8 - Javascript

They are the same. Just as [] and new Array() are the same.

For more information on the core types of JavaScript, have a look at the MDC Core JavaScript 1.5 reference.

If you want proof that {} is the same as new Object():

Object.prototype.helloWorld = function () { alert('Foo!'); };
var a = new Object();
var b = {};
a.helloWorld();
b.helloWorld();

!!! WARNING ACHTUNG AVERTISSEMENT !!! Never, ever assign to the prototype property of the Object type in production code. You'll be polluting the whole global namespace.

Solution 9 - Javascript

Technically, they are the same. When you write code, you can easily do myobject['someproprty' + 'somethingElseConcatenated], which you cannot do when using the "dot notation" - myobject.someproperty is all you can do.

Douglas Crockford, one of autors of ECMAscript, suggests not to use var a = new Object() syntax for some reason I didn't quite catch. Anyway, it's worth watching his presentation if you're interested in it (it consists of several parts, the first one is here http://video.yahoo.com/watch/111593/1710507)

Solution 10 - Javascript

Every engine(browser) implements it differently but let focus on chrome's V8(based on performance tests I performed a year ago on most of the modern browsers they give similar performance boosts if you follow v8 guidlines).

What it basically happens is:

  1. To implement a dynamic object on which properties can be added and deleted on the fly - a hashtable is the simplest solution - but speedwise its not as efficient as a regular object in java(random access...).
  2. What V8 does is trying to guess based on few strategies if your object is a regular object(has final set of properties set in specific order, etc...) or a hashtable. At first it assumes this is a simple object and each new property causes creation of a new structure of an object and copying of the old one on it plus the new property. If the object is categorize as "difficult" - it is moved to a hushtable.
  3. If v8 notices too many "mistakes" - it moves everything to hashtables - therefore you get poor performance (thats why you want to use constructors that init all of your members or structures inited in a json...)

Please see this links: https://developers.google.com/v8/design#prop_access

www.quora.com/Are-JavaScript-Objects-implemented-as-HashTables-Is-key-value-access-O-1

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

Also a very good lecture: https://www.youtube.com/watch?v=UJPdhx5zTaw

Hope it helps...

Solution 11 - Javascript

Actually, there is nothing called 'hashtable' or 'hashmap' in JavaScript. The object in JavaScript behaves like a 'hash' [objects in JavaScript are simply key/value properties] and hence the confusion.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLandon KuhnView Question on Stackoverflow
Solution 1 - JavascriptMarcinView Answer on Stackoverflow
Solution 2 - JavascriptIan ElliottView Answer on Stackoverflow
Solution 3 - JavascriptTim DownView Answer on Stackoverflow
Solution 4 - JavascriptThevsView Answer on Stackoverflow
Solution 5 - JavascripttvanfossonView Answer on Stackoverflow
Solution 6 - JavascriptJimmy ChandraView Answer on Stackoverflow
Solution 7 - JavascriptTheHippoView Answer on Stackoverflow
Solution 8 - JavascriptBlixtView Answer on Stackoverflow
Solution 9 - JavascriptnaivistsView Answer on Stackoverflow
Solution 10 - JavascriptMax LeizerovichView Answer on Stackoverflow
Solution 11 - JavascriptSolutionYogiView Answer on Stackoverflow