Data Structures: Create a Map Data Structure

Challenge: Create a Map Data Structure
Create the following methods and operations on the Map object:

  • add accepts a key, value pair to add to the map.
  • remove accepts a key and removes the associated key, value pair
  • get accepts a key and returns the stored value
  • has accepts a key and returns true if the key exists or false if it doesn’t.
  • values returns an array of all the values in the map
  • size returns the number of items in the map
  • clear empties the map


var Map = function() {
  this.collection = {};
  this.add = (key,value)=>{
    this.collection[key] = value;

  this.remove = (key)=>{    
    this.has(key) ? delete this.collection[key] : false;

  this.has = (key)=>{
    return this.collection.hasOwnProperty(key);

  this.get = (key)=> {
      return this.collection[key];

  this.values = ()=>{
    return Object.values(this.collection);
  this.size = () => {
    return this.values().length;
  this.clear = () =>{
    for(let el in this.collection){
      delete this.collection[el];

Link to the challenge:
Map Data Structure

For extra credit:

Compare the performance of your Map structure with the one built in to ES6.

Insert a whole bunch of objects with random-number keys, then time how long it takes to do the various functions like .has(), .get(), .remove() and so forth.

Based on your measurements, state the pros and cons of your implementation over the built-in one.

1 Like