Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a JavaScript hash map implemented?

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?

like image 766
Patrick Hillert Avatar asked Jan 16 '12 09:01

Patrick Hillert


People also ask

How is HashMap implementation?

Hashmap uses the array of Nodes(named as table), where Node has fields like the key, value (and much more). Here the Node is represented by class HashMapEntry. Basically, HashMap has an array where the key-value data is stored. It calculates the index in the array where the Node can be placed and it is placed there.

Whats a HashMap in JavaScript?

Hashmap, also known as Hash Table, is a collection of elements where elements are stored in the form of a key-value pair. There was no predefined data structure available for hashmap implemented in the JavaScript programming language back in the day.

Can we use HashMap in JavaScript?

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.

How does JavaScript map work?

The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method. Generally map() method is used to iterate over an array and calling function on every element of array.


2 Answers

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.

like image 165
otakustay Avatar answered Sep 21 '22 21:09

otakustay


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.

like image 34
Craig Barnes Avatar answered Sep 19 '22 21:09

Craig Barnes