Hash Tables
A hash table is an implementation of an associative array, a list of key-value pairs that allow you to retrieve a value via a key. Internally a hash table utilizes a hash function to transform a key value into an index that points to where the value is stored in memory. Hash tables have fast search, insertion and delete operations.
There are two main ways to implement a hash table/associative array in JavaScript.
Using the Object Data Type
The simplest implementation is using the Object
data type. This is because all non-scalar objects in JavaScript behave as associative arrays, a mapping from property keys to values. So an Object
itself can behave as a basic hash table.
var simplehash = new Object();// or// var simplehash = {};simplehash['key1'] = 'value1';simplehash['key2'] = 'value2';simplehash['key3'] = 'value3';for (var key in simplehash) {// use hasOwnProperty() to filter out properties from Object.prototypeif (simplehash.hasOwnProperty(key)) {console.log('key is: ' + key + ', value is: ' + simplehash[key]);}}
The output would look like:
key is: key1, value is: value1key is: key2, value is: value2key is: key3, value is: value3
There are some downsides to this approach:
- The
Object
comes with its own properties which could collide with potential key names. - There no easy way to get the size of a Hash Table stored in an
Object
, so it must be tracked manually. - Since they are also property names, the keys used are limited to
String
orSymbol
types. Object
isn’t optimized for frequent additions and removals of key-value pairs
Using a Map Object
The Map
object was created to implement this type of associative array without some of the downsides of using a basic Object
:
- There are no pre-existing keys that could result in a collision
- A
Map
object has asize
property to track its contents. - A
Map
object can have keys that are any data type. - A
Map
has been optimized for repeated addition and insertion of key-value pairs.
A Map
object also comes with the following methods:
.clear()
Removes all key-value pairs from theMap
object..delete(key)
Deletes the key-value pair and returnstrue
if the key exists. Returnsfalse
otherwise..get(key)
Returns the value associated with key, orundefined
if key doesn’t exist..has(key)
Returnstrue
if key exists,false
otherwise..set(key,value)
Sets the value for the key in theMap
object and returns theMap
object.
Note: Key-value pairs must be set with the set
method in order for the Map
object to behave as expected. Using the syntax for Object
above will appear to work, but will not associate the key-value pair to its internal collection.
var maphash = new Map();maphash.set('key1', 'value1');maphash.set('key2', 'value2');maphash.set('key3', 'value3');console.log(maphash.get('key3'));// Output: value3maphash.set('key1', 'new value');console.log(maphash.get('key1'));// Output: new valueconsole.log(maphash.size);// Output: 3maphash.delete('key2');console.log(maphash.size);// Output: 2for (const [key, value] of maphash) {console.log(key + ' = ' + value);}// Output: key1 = new value// key3 = value3
Codebyte Example
Run the following code to understand the concept of hash tables:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Skill path
Create a Back-End App with JavaScript
Learn how to build back-end web APIs using Express.js, Node.js, SQL, and a Node.js-SQLite database library.Includes 8 CoursesWith CertificateBeginner Friendly30 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours