A custom implementation of HashMap and HashSet data structures built from scratch in JavaScript.
This project focuses on understanding how hash-based data structures work internally, including hashing, load factors, dynamic resizing, and efficient data storage.
The goal of this project is to recreate the core behavior of hash maps and hash sets without relying on JavaScript’s built-in Map or Set. Both implementations use a fixed-size bucket array, a custom hash function, and automatic resizing when a predefined load factor is exceeded.
Insertion order is not preserved, which is expected behavior for hash-based data structures.
The HashMap stores key–value pairs using string keys only.
set(key, value)— Adds a new key–value pair or updates an existing keyget(key)— Returns the value associated with a key, ornullif not foundhas(key)— Returnstrueif the key exists, otherwisefalseremove(key)— Removes a key and returnstrueif successfullength()— Returns the number of stored entriesclear()— Removes all entrieskeys()— Returns an array of all stored keysvalues()— Returns an array of all stored valuesentries()— Returns an array of[key, value]pairs
The HashSet stores unique string keys without associated values.
It follows the same hashing and resizing logic as the HashMap implementation.
set(key)— Adds a key if it does not already existget(key)— Returns the key if found, otherwisenullhas(key)— Returnstrueif the key existsremove(key)— Removes a key and returnstrueif successfullength()— Returns the number of stored keysclear()— Removes all keyskeys()— Returns an array of stored keys
- Buckets are stored in a fixed-size array
- A prime-number-based hash function is used
- Modulo is applied during hashing to prevent integer overflow
- The load factor is set to
0.75 - When the load factor is exceeded, capacity is doubled and all entries are re-hashed
- Keys are limited to strings only