0% found this document useful (0 votes)
4 views9 pages

Hash_maps_in_JavaScript

The document provides an overview of hash maps in JavaScript, highlighting their role as a data structure that stores key-value pairs for efficient data retrieval. It explains the differences between hash maps and arrays, the advantages of using hash maps, and the importance of hash functions in ensuring data security. Additionally, it covers built-in methods for hash maps, time and space complexity, and strategies to avoid collisions.

Uploaded by

dblue4396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Hash_maps_in_JavaScript

The document provides an overview of hash maps in JavaScript, highlighting their role as a data structure that stores key-value pairs for efficient data retrieval. It explains the differences between hash maps and arrays, the advantages of using hash maps, and the importance of hash functions in ensuring data security. Additionally, it covers built-in methods for hash maps, time and space complexity, and strategies to avoid collisions.

Uploaded by

dblue4396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Hash maps in JavaScript

Data Structures organize and store data in a format that provides for efficient
retrieval.

in JavaScript data structure types include:

objects.

arrays.

stacks.

trees.

graphs.

hash tables (hash map).

JavaScript is an object-oriented programming language. Hash Tables are a


type of object and a data structure that allows for the creation of lists of paired
values.

Hash maps in JavaScript 1


//constructor function for Map Object
let maps = new Map ([
['kabc', 1],
['kdef', 2],
['gei', 3]
]);

console.log(maps);
//map { 'kabc': 1, 'kdef': 2, 'gei': 3}

Map Objects ⇒ are collections of key-value pairs that can be functions,


objects and any data type, they are efficient in not only storage but retrieval of
values.

it provides : space and time complexity.

The difference between Arrays and Hash maps in


JavaScript ?
hash tables or hash maps are a type of object and are easily implemented by
declaring a new object. Hash tables possess key value pairs that allow for easy
retrieval with a key. Hash tables utilize hash functions that are created by
mathematicians. And the functions are used to change the key value to an
index that will point to the location where the value will be stored in memory.
This data structure is known for being efficient in search, insert ,and delete.

Arrays are useful data structure in JavaScript that are zero indexed and used to
store a collection of items within one variable name. Arrays in JavaScript have
several built-in functions, also known as methods, as arrays under the hood
are actually objects as well. However, they differ from hash maps as arrays can
be resized, and the traversal of arrays is slightly different than hash maps. The
time complexity of each is about the same as both are very different than hash

Hash maps in JavaScript 2


maps. The time complexity of each is about the same as both are very efficient
for retrieval of data depending on the space you use and data types within the
structure.
the takeaway is that hash maps are like arrays as they are both objects under
the hood, but they differ in some ways such as traversing and indexing. There
are several other similarities and differences, but the valuable new knowledge
is that hash maps are useful data structure in JavaScript.

why use Hash map ?


Hash maps offer several advantages that make them a popular choice in
programming:

1. Fast data retrieval: Hash maps provide constant-time O(1) complexity for
insertion, deletion, and lookup operations, making them highly efficient for
large datasets.

2. Flexible key-value pairs: They allow you to associate any type of key with any
type of value, providing versatility in data organization.

hash maps have keys and they are necessary for data storage and are
necessary for the hash functions on data retrieval.
hash maps are needed when a unique keys are needed.

Hash maps in JavaScript 3


e.g. ⇒ when we need to create a phone book application. where the name will
be the key and the value will be the phone number.

Do I have to Know How to create a hash


function ? No

hash functions (hash algorithm ) : are calculation to an input key that will
transform it into an address where the data or value will be stored.

the algorithm is created by a mathematician and can be utilized to safely


store values.

Hash functions can be reused and have certain properties.

a Hash function should be deterministic meaning it yields the same output


every time with the same input.

Hash functions are also irreversible, and this feature makes hash tables an
asset in cybersecurity. because they are useful in storing passwords and
very sensitive data.

the hash function should be Collison resistance.

Hash maps in JavaScript 4


Collision ⇒ happens when the hash function yields the same address
for a value so two data are stored in the same address.

ways to avoid a hashing collision:

collision chaining : where length lists are used to store values on


different nodes at the same address.

Open addressing : is when a collision occurs the next available address


is found and the value is placed there.

well known hash functions :

SHA-256: in cybersecurity and its the secret hash algorithm 2 was


created by the NSA.

Hash maps built-in methods:


get(’key’) ⇒ returns the value that is paired with the key, otherwise it’ll return
undefined if the key doesn’t exist.

example :

Hash maps in JavaScript 5


in hash maps data can be retrieved using the key

set (’key’,’value’) ⇒ returns

it’s used to add data to the hash table.

let mapHash = new Map ();


//entering the data
mapHash.set('Precious','german shepherd');
mapHash.set('Sushi','Poodle');
mapHash.set('Petunia','retriever');
mapHash.set('lilly','husky');

//retreiving
console.log(mapHash.get('Sushi'));
//here its stored using the hash function to add security

console.log(mapHash.size);

Remember that when using the hash function a bucket number is assigned
for the data, and it is a different bucket number for each entry.

.clear () ⇒ to clear the hash table.

Hash maps in JavaScript 6


.delete(’key’) ⇒ to erase the contents and data that you no longer need. to delete
a specific key and value.

Hashing ⇒ is the process in which a key is placed in a hash function and then the
hash function carries out its functionality, thereby creating an index and placing
the keys and their values in a bucket.

hashing is always carried out to avoid collisions, which is where already filled
buckets are used by a hashing function to store additional data from a different
key input.

Hash Functions:

= !!! Important Notes:

No matter the length of the input the output length is the same.

Even small changes generate a completely different output.

Hash maps in JavaScript 7


the same input will always have the same output.

There is NO Reverse Engineering !!! you cannot go from the hash to the input.

Security of data is important, and hash functions offer another layer of protection
for confidential company data.

Dictionary cyberattack: is when a hacker creates a dictionary or table with


common passwords and runs them through a well-known hash function.

Time and Space Complexity:

Hash maps in JavaScript 8


Big O notation aka big O ⇒ is a term that is common among developers and is
in relation to the running time of a function.

Space Complexity : is the total amount of space taken by the algorithm, reliant
upon the input size.

it includes :

auxiliary space.

space used by the function input.

for a hash table its O(n) .

Time Complexity : is the amount of time taken by an algorithm to run, as a


function of the length of the input. For hash tables, the average time
complexity for operations like insertion, deletion, and lookup is O(1), which
means constant time. However, in the worst-case scenario (when many
collisions occur), it can degrade to O(n).

These efficiency characteristics make hash tables particularly useful for


large datasets where quick access and modification are crucial.

Hash maps in JavaScript 9

You might also like