Skip to main content

How JavaScript uses hashing



  • What is hashing and what it is used for?

There are many important reasons for using hashing. One application of hashing is using it to store passwords in databases. You may need to store your user’s password so that you know whether they have entered a valid password or not. However, it would be very insecure to store all passwords as plain text in the database. Rather, it would be more secure if you applied a hashing function to the password and stored the hash/digest in the database instead. 

Hashing can also be used for version control. When given the same input, a specific hashing algorithm will always produce the same digest. Therefore, we can compare the digest of two separate files and if the digests are the same, we will know the files are the same and have not been changed. 


  • What is a hash table and what are the benefits of this data structure?

A hash table, also known as a hash map, is a data structure that maps keys to values. It is one part of a technique called hashing, the other of which is a hash function. A hash function is an algorithm that produces an index of where a value can be found or stored in the hash table.

Some positive aspects of hash tables are that hash tables are a fast and efficient way for looking up, creating, and deleting stored data. Specifically, hash tables are typically more efficient for looking up values than search trees, which have a linear time complexity, and binary search trees, which have a logarithmic time complexity. Regardless of the input size, hast tables will typically have a constant time complexity for looking up, creating, and deleting stored data.

  • The difference is between hashing and encryption explained

Hash Functions provide a mapping between an arbitrary length input, and a (usually) fixed length (or smaller length) output. It can be anything from a simple crc32, to a full blown cryptographic hash function such as MD5 or SHA1/2/256/512. The point is that there's a one-way mapping going on. It's always a many:1 mapping (meaning there will always be collisions) since every function produces a smaller output than it's capable of inputting (If you feed every possible 1mb file into MD5, you'll get a ton of collisions). 

Encryption Functions provide a 1:1 mapping between an arbitrary length input and output. And they are always reversible. The important thing to note is that it's reversible using some method. And it's always 1:1 for a given key. Now, there are multiple input:key pairs that might generate the same output (in fact there usually are, depending on the encryption function). Good encrypted data is indistinguishable from random noise. This is different from a good hash output which is always of a consistent format.


  1. What is a Map object is

Map objects are collections of key/value pairs where both the keys and values may be arbitrary ECMAScript language values. A distinct key value may only occur in one key/value pair within the Map’s collection. Distinct key values are discriminated using the SameValueZero comparison algorithm.

Map objects must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structures used in this Map objects specification is only intended to describe the required observable semantics of Map objects. It is not intended to be a viable implementation model.


Syntax:

As the map is an object in JavaScript, it is defined by using new keyword.

let map = new Map();

  1. Compare objects and maps

Map is a data structure which helps in storing the data in the form of pairs. The pair consists of a unique key and a value mapped to the key. It helps prevent duplicity.
Object follows the same concept as that of map i.e. using key-value pair for storing data. But there are slight differences which makes map a better performer in certain situations.

23.1

Few basic differences are as follows:

  1. In Object, the data-type of the key-field is restricted to integer, strings, and symbols. Whereas in Map, the key-field can be of any data-type (integer, an array, even an object!)
  2. In the Map, the original order of elements is preserved. This is not true in case of objects.
  3. The Map is an instance of an object but the vice-versa is not true.

Comments