How JavaScript Uses Hashing

Photo by George Marwanqana from Medium

Welcome back to another web development topic! Today we will be talking about hashing in JavaScript. This is a popular concept since many modern programming languages today have built-in hash tables, which makes these languages easy to use, for example in JavaScript it is the JavaScript object that we use. We will discuss this in greater depth later on in this article. First, lets start off with the basics.

What is hashing?

Hashing is the process of converting a given key into another value. Hashing refers to the process of converting object data into a representative integer value via a function or algorithm. Then, when looking for the object on the map, we can utilize this hash code to focus our search. These hash codes are typically used to create an index where the value is kept.

What is hashing used for?

Hashing is designed to solve the 2 main problems, one of which is having to rapidly retrieve or store an item in a collection and the second is for the purpose of digital signatures. Let's discuss this is further detail.

Data retrieval

Let's imagine that we have a list of 1000 words and we want to find a specific word on that list, it would be a time consuming and ineffective method to discover a match. Therefore, we introduce hashing which is a technique for boosting output by initially successfully limiting the search. By utilizing algorithms or functions, hashing will convert object data into a useful integer value. The search for these objects on that object data map can then be honed using a hash. Developers store data, such as a customer record, as key and value pairs in hash tables. The hash code is then mapped to a predetermined size, while the key aids in data identification and serves as an input to the hashing method.

Digital signatures

Digital signatures can be encrypted and decrypted with the use of hashing. This means that in addition to facilitating speedy data retrieval, it is required to verify people who send messages and recipients. In this instance, a hash function modifies the digital signature before the hashed value and the signature are sent separately to the recipient. Therefore, the same hash function is used to produce the message from the signature when a message is received. This message digest is then compared to the message that was sent to ensure that they are the same. As a result, the hash function indexes the initial value or key and makes data linked to a particular value or key that is retrieved accessible in a one-way hashing operation.

What is a hash table?

We will now talk about the data structure called a hash table which uses associative coding to store data. Each data value in an array format is kept in a hash table with a distinct index value. Hash tables keep data in the form of key-value pairs and it has to support 3 functions that is to insert the key and value, get the key and delete the key. If we are aware of the index for the desired data, access to it becomes quite quick. As a result, it develops into a data structure in which insertion and search operations, regardless of the size of the data, are very quick. Overall, the hash table uses an array as its store medium and generates an index from which an element can be located or inserted using a hashing algorithm.


Photo by George Marwanqana from Medium 

Now that we know hash tables store key-value pairs, lets looks at the image above for a better understanding. We want to store the names and contact numbers of a few friends, so which entity exactly would be the key and the value? Well, the key could be the our friend's name and the value could be his/her contact number.

What are the benefits of a hash table?

The benefit of hash tables over other data structures is speed. This is because the maximum possible amount of entries can be determined in advance and at a quick pace, thereby making hash tables effective. Hash tables perform well even when working with huge datasets because their time complexity is constant regardless of the number of items in the table. Therefore hash tables have a good performance rate whilst looking up data, entering entries, and deleting existing ones.

What is the difference between hashing and encryption?

Hashing and encryption are frequently compared in the context of data security, but why? Despite having similar looks, they are different from one another. Let us differentiate between the two concepts. Hashing is helpful for confirming the authenticity of the content because it detects all modifications and generates a hash value as an output. On the other hand, data can be encrypted using encryption to ensure its security and confidentiality. Therefore a private key is needed to decipher the encrypted data. Furthermore, when data is passed in as plaintext during encryption and is output as ciphertext, it cannot be read. Data can however be decrypted so that it can be read since encryption is two-way whilst hashing is one-way.

What is a Map object and how can it be used?

The Map object stores key-value pairs and keeps track of the keys' initial insertion order. It is important to note that any value can serve as a key or a value, including objects and primitive values. In maps, things can also serve as keys. With JavaScript objects, this is not the case. JavaScript objects only permit the usage of primitives as keys. Iterability is another quality that makes it beneficial in some circumstances. A map object can therefore be used in situations when it is necessary to preserve the order of keys and have them correspond to values.

What is the difference between objects and maps?

A map is a type of data structure that makes it possible to store data as pairs. A single key and a value that is mapped to the key make up the pair. It guards against deceit. The idea behind an object is similar to that of a map in that data is stored using key-value pairs. However, there are a few minor variations that help maps function better in some circumstances.

Three fundamental differences between objects and maps are as follows:
1. The key field's data type in objects are limited to integer, strings, and symbols. In contrast, the key
field in a map can include any type of data, including objects and arrays.
2. Maps maintains the elements original order but with objects this is not true.
3. A Map is known to be an instance of an object, however the vice-versa is not true.

How can hashing be used for objects and maps?

Objects in JavaScript are a unique form of Hash Table implementation which now has object class-added properties. This means that the keys you enter can clash and change the class's inherited default properties. Additionally, there is no tracking of the Hash Table's size. Therefore, how many properties are defined by the programmer as opposed to being inherited from the prototype must be manually counted.

Similar to objects, maps enable the storage of key-value pairs within the data structure. For any key-pair values we want to add to the data structure, by it is important to first define them using the set() and get() methods, unlike in objects. Additionally, a map inherited properties cannot be changed.

Conclusion

In this article we have discussed some important points on how JavaScript uses hashing. Overall, we can say that the three main reasons for implementing hashing is to improve security, provide version control, and build more effective data structures.

I hope you have enjoyed this article.

Thank you for reading!

Sources

Data Structures Handbook - Hash Tables
freeCodeCamp - What is Hashing? How Hash Codes Work - with ExamplesMedium - How JavaScript uses hashing by George Marwanqana
MDN Web docs - Map
TechTarget - Hashing by Andrew Zola

Comments