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?
This question is related to
javascript
hash
hashmap
Should you try this class Map
:
var myMap = new Map();_x000D_
_x000D_
// setting the values_x000D_
myMap.set("1", 'value1');_x000D_
myMap.set("2", 'value2');_x000D_
myMap.set("3", 'value3');_x000D_
_x000D_
myMap.size; // 3_x000D_
_x000D_
// getting the values_x000D_
myMap.get("1"); // "value associated with "value1"_x000D_
myMap.get("2"); // "value associated with "value1"_x000D_
myMap.get("3"); // "value associated with "value3"
_x000D_
Notice: key and value can be any type.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
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.
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.
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 .....
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.
<html>
<head>
<script type="text/javascript">
function test(){
var map= {'m1': 12,'m2': 13,'m3': 14,'m4': 15}
alert(map['m3']);
}
</script>
</head>
<body>
<input type="button" value="click" onclick="test()"/>
</body>
</html>
Source: Stackoverflow.com