How JavaScript Uses Hashing

 



                                                                                                   (Sentavio, 2021)

What is Hashing?

Hashing is the process of taking a key or string and converting it into another value. It was designed to remedy the problem of needing to store or find data in a collection. For example, if user input their name and password to be stored, you would hope that the database doesn't store your password as is. This would be incredibly unsafe. This is where hashing comes in. Hashing is used for data retrieval as well as digital signatures (used to authenticate senders and receivers). There are multiple types of hashing, but the most common are MD5, SHA-2 and CRC32.

Hash Tables

Hash tables are a type of data structure that is used to efficiently store data and uses an unordered key-value storage method. The data is mapped to positions in an array by the hash function of choice that generates a unique value from the key. The real advantage of this data structure is speed. In normal circumstances, the average time to execute the search in a hash table is O(1) Constant Time thus lookup is performed relatively fast.

                                                                                                      (Shahrior, 2021)

Hashing vs. Encryption

A common question that arises when discussing hashing is how is it different from encryption as both change raw data into a different format. Hashing provides a hash value and encryption provides ciphertext. Usage of the two is a key difference between the two. Hashing is used to determine the integrity of file during transfer over a network, to securely store data in a hash table and for indexing large structures/objects, whereas encryption is used mainly to protect data. Another important difference is that hashing is not reversible whereas encryption is, that being said hashing is more secure than encryption.

Map Objects and Hashing

In JavaScript, Map is the collection of elements where every element is stored as a key-value pair. Map object can hold both objects and primitive values as either key or value. Map object allows us to use key-value pairs of any type, not just string or symbol, as either key or values. It also remembers the original insertion order of the keys. Example of creating a new Map object: 

                                                                                                                                                     (JavaScript Tutorial, 2021)


Map and Object are similar as they both allow you to set keys to values, lookup values, delete keys and find if something is stored at a key. That being said there are also key differences. As previously mentioned, Object limits use to keys of either string or symbol whereas Map allows for keys to be any value. Object comes standard with default keys which Map does not contain. Performance-wise Map performs better in instances where constant or frequent addition and removal of key-value pairs is required. Object is a good choice when we simply want to store our data and know that all keys are either strings or integers (hashed) whereas Map performs remarkably better in cases where we want to add and remove key-value pairs frequently. Map also outperforms Object when it comes to storing large sets of data. 

Ultimately it depends on what kind of input data you are going to work with or what operation you are going to execute to determine which option, Map or Object, is preferred. 

Sources: 

B, M., 2011. Fundamental difference between Hashing and Encryption algorithms. [online] Stack Overflow. Available at: <https://stackoverflow.com/questions/4948322/fundamental-difference-between-hashing-and-encryption-algorithms> [Accessed 28 September 2021].

Chung, C., 2021. What is Hashing? Benefits, types and more. [online] 2brightsparks.com. Available at: <https://www.2brightsparks.com/resources/articles/introduction-to-hashing-and-its-uses.html> [Accessed 28 September 2021].

Developer.mozilla.org. 2021. Map - JavaScript | MDN. [online] Available at: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map> [Accessed 28 September 2021].

Data Structures Handbook. 2021. Hash Tables - Data Structures Handbook. [online] Available at: <https://www.thedshandbook.com/hash-tables/> [Accessed 28 September 2021].

Garg, P., n.d. Basics of Hash Tables Tutorials & Notes | Data Structures | HackerEarth. [online] HackerEarth. Available at: <https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/tutorial/> [Accessed 28 September 2021].

GeeksforGeeks. 2021. Difference between Hashing and Encryption - GeeksforGeeks. [online] Available at: <https://www.geeksforgeeks.org/difference-between-hashing-and-encryption/> [Accessed 28 September 2021].

GeeksforGeeks. 2021. Map in JavaScript - GeeksforGeeks. [online] Available at: <https://www.geeksforgeeks.org/map-in-javascript/> [Accessed 28 September 2021].

JavaScript Tutorial. 2021. The Essential Guide to JavaScript Map: How To Use Maps Effectively. [online] Available at: <https://www.javascripttutorial.net/es6/javascript-map/> [Accessed 28 September 2021].

Shavin, M., 2018. ES6 — Map vs Object — What and when?. [online] Medium. Available at: <https://medium.com/front-end-weekly/es6-map-vs-object-what-and-when-b80621932373> [Accessed 28 September 2021].

Study.com. 2021. Hash Tables: Definition, Use & Functions. [online] Available at: <https://study.com/academy/lesson/hash-tables-definition-use-functions.html> [Accessed 28 September 2021].

Zola, A., 2021. What is hashing and how does it work?. [online] SearchSQLServer. Available at: <https://searchsqlserver.techtarget.com/definition/hashing> [Accessed 28 September 2021].

Popular Posts