119

I currently work with OpenLayers and have a huge set of data to draw into a vector layer (greater than 100000 vectors).

I'm now trying to put all these vectors into a JavaScript hash map to analyze the performance. I want to know how is the hash map in JavaScript implemented, is it a real hash function or just a wrapped function that uses a simple data structure and a search algorithm?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Patrick Hillert
  • 2,309
  • 4
  • 22
  • 37
  • 3
    There's not just one JS implementation, so there's no way to answer this. ECMAScript doesn't specify what data structure to use for objects, nor does it specify restraints on access time. Hashes are typical, but balanced trees could be used. – outis Jan 16 '12 at 09:31
  • 1
    ES6 have pure Maps. The link describes differences between plain object and Map, key details etc: [MDN JavaScript Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) – Den Roman Jan 18 '17 at 15:23

7 Answers7

236

every javascript object is a simple hashmap which accepts a string or a Symbol as its key, so you could write your code as:

var map = {};
// add a item
map[key1] = value1;
// or remove it
delete map[key1];
// or determine whether a key exists
key1 in map;

javascript object is a real hashmap on its implementation, so the complexity on search is O(1), but there is no dedicated hashcode() function for javascript strings, it is implemented internally by javascript engine (V8, SpiderMonkey, JScript.dll, etc...)

2020 Update:

javascript today supports other datatypes as well: Map and WeakMap. They behave more closely as hash maps than traditional objects.

kabirbaidhya
  • 3,264
  • 3
  • 34
  • 59
otakustay
  • 11,817
  • 4
  • 39
  • 43
36

JavaScript objects cannot be implemented purely on top of hash maps.

Try this in your browser console:

var foo = {
    a: true,
    b: true,
    z: true,
    c: true
}

for (var i in foo) {
    console.log(i);
}

...and you'll recieve them back in insertion order, which is de facto standard behaviour.

Hash maps inherently do not maintain ordering, so JavaScript implementations may use hash maps somehow, but if they do, it'll require at least a separate index and some extra book-keeping for insertions.

Here's a video of Lars Bak explaining why v8 doesn't use hash maps to implement objects.

Craig Barnes
  • 159
  • 8
Craig Barnes
  • 361
  • 3
  • 4
  • 4
    "otakustay is technically wrong, the worst kind of wrong." That's a little harsh. It may not be 1:1, but for the intents and purposes of using a hash like a dictionary, it works in the same fashion. – probablyup Jan 27 '14 at 20:45
  • 1
    Just want to clarify that this may be true of some implementations of JavaScript (such as most browsers) but not necessarily always true. The order of iteration over keys is not defined by the ECMAScript standards and may be any order and still be a valid JS implementation. – TheZ May 19 '15 at 23:40
  • Separately from the issue of whether some implementations use hashing, it is not really relevant to the original question whether or not they also keep an insertion-ordered list of keys for user convenience. – Craig Hicks Jan 14 '21 at 18:35
24

Should you try this class Map:

var myMap = new Map();

// setting the values
myMap.set("1", 'value1');
myMap.set("2", 'value2');
myMap.set("3", 'value3');

console.log(`Map size: ${myMap.size}`); // 3

// getting the values
console.log(`Key: "1", Value: ${myMap.get("1")}`);    // "value associated with "value1"
console.log(`Key: "2", Value: ${myMap.get("2")}`);    // "value associated with "value2"
console.log(`Key: "3", Value: ${myMap.get("3")}`);    // "value associated with "value3"

Notice: key and value can be any type.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

zhulien
  • 5,145
  • 3
  • 22
  • 36
Nguyen Tan Dat
  • 3,780
  • 1
  • 23
  • 24
20

Here is an easy and convenient way of using something similar to the Java map:

var map= {
    'map_name_1': map_value_1,
    'map_name_2': map_value_2,
    'map_name_3': map_value_3,
    'map_name_4': map_value_4
    }

And to get the value:

alert( map['map_name_1'] );    // fives the value of map_value_1

......  etc  .....
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
8

While plain old JavaScript objects can be used as maps, they are usually implemented in a way to preserve insertion-order for compatibility with most browsers (see Craig Barnes's answer) and are thus not simple hash maps.

ES6 introduces proper Maps (see MDN JavaScript Map) of which the standard says:

Map object must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection.

mb21
  • 34,845
  • 8
  • 116
  • 142
0

function test() {
  var map = {
    'm1': 12,
    'm2': 13,
    'm3': 14,
    'm4': 15
  }
  alert(map['m3']);
}
<input type="button" value="click" onclick="test()" />
Not A Bot
  • 2,474
  • 2
  • 16
  • 33
0

I was running into the problem where i had the json with some common keys. I wanted to group all the values having the same key. After some surfing I found hashmap package. Which is really helpful.

To group the element with the same key, I used multi(key:*, value:*, key2:*, value2:*, ...).

This package is somewhat similar to Java Hashmap collection, but not as powerful as Java Hashmap.

dd619
  • 5,910
  • 8
  • 35
  • 60