Hashtable in javascript: Objects vs Maps()

Hashtable in javascript: Objects vs Maps()

·

3 min read

Hashtable is a basic data structure used to store key-pair values. It is especially efficient and a need to know for solving algorithmic questions and even displaying data on your website.

Complexity Analysis of a hashtable

Worst CaseBest case
Inserting0(n)0(1)
Deletion0(n)0(1)
Lookup0(n)0(1)

A simple hashtable might look like this

{
  'a,e,t': [ 'eat', 'tea', 'ate' ],
  'a,n,t': [ 'tan', 'nat' ],
  'a,b,t': [ 'bat' ]
}

// where the key is a string -> 'a,e,t'
// and the value is an array

or using a map

Map(2) {
  'a,e,t' => [ 'eat', 'tea', 'ate' ],
  'a,n,t' => [ 'tan', 'nat' ],
  'a,b,t' => [ 'bat' ]
}

You might be more familiar with the structure of the first code snippet which is a javascript object than the second one which is a Map object. However, they both do similar work in that they store key-value pairs in javascript, allowing values to be easily accessible and support quick insertion.

What then is the difference?

Or better yet, if they have the same complexity analysis. What might be their differences and how do I decide to use one of the other while implementing an algorithm solution?

Great question, I'm glad you asked.

They are both great data structures for hash tables. They differ in how they handle different operations though. Let's have a look:

1. Definition.

Define an object

let hash = {} // a simple object structure

Define a Map()

let map = new Map()

2. Retrieval

Retrieve a value from an object

The value of a key can be retrieved by wrapping the key in square brackets

let hash = {
  tea: 'black',
  bread: 'whole grain',
  coffee: 'espresso'
};

 let target = 'tea'

  console.log(hash[target]) //black

retrieve a value in a map using .get()

//  Retrieval - MAP
let map = new Map([
    ['tea', 'black'],
    ['bread', 'whole grain'],
    ['coffee', 'espresso']
]);
 let target = 'bread'

  console.log(map.get(target)) // whole grain

3. Insertion

Insert a key-value pair into an object

let hash = {
  tea: 'black',
  bread: 'whole grain',
  coffee: 'espresso'
};

// insert a new pair 'salad:near' into the hashtable

hash['salad'] = 'neat'
console.log(hash)

'''
{
  tea: 'black',
  bread: 'whole grain',
  coffee: 'espresso'
  salad: 'neat'
};
'''

Insert a key-value pair into a Map

let map = new Map([
    ['tea', 'black'],
    ['bread', 'whole grain'],
    ['coffee', 'espresso']
]);

map.set('salad', 'neat') // insert using .set() method which takes in 2 values - the key and the object
console.log(map)

'''
Map(4) {
  'tea' => 'black',
  'bread' => 'whole grain',
  'coffee' => 'espresso',
  'salad' => 'neat'
}
'''

4. looping over key...value pairs

In object

let hash = {1: 3, 2: 4, 3:1}

for(let[key, value] of Object.entries(hash)){
    // perform operation
}

Using map

let map = Map(3) {1 => 3, 2 => 4, 3 => 1}

for(let[key, value] of map){
    // perform operation
}

5. Returning values

Using object

let hash = {1: 3, 2: 4, 3:1}

return Object.values(hash)

Using Map

let map = Map(3) {1 => 3, 2 => 4, 3 => 1}

return map.values()

Hashtables in javascript can be implemented using objects {} and Map(), however, there are syntax tradeoffs you might decide on depending on the algorithm operation you want to perform.

Happy hacking coders🥷