How JavaScript uses hashing
INTRODUCTION
Hashing is the transformation of a string of characters into a generally more limited fixed-length value or key that represents the first string. Hashing is used to index and retrieve items in a database because it is faster to find the item using the shorter hashed key than to find it using the original value. It is also utilized in numerous encryption algorithms.
1. Controlling speed by choosing the space to be allocated for the hash table2. Controlling space by picking a speed of recuperation
What a hash table is and what the benefits of this data structure are
What the difference is between hashing and encryption.
- In contrast to encryption, hashing serves as a checksum to guarantee that a specific piece of data or a document hasn't been modified.
- Hashing is the most reasonable way to safely store passwords. By putting away passwords in a good hash design, it's practically impossible for anybody to get to your raw data.
- Hashing is useful in comparing a value and with a stored value hence evading duplication. This can be done by putting away the hash with a salt, and then with any future login attempts, hash the passwords that the clients enter and compare it and the put away hash.
- Hashing is utilized in an variety of digital certificates, including SSL certificates. Hashing helps you find specific data in a huge database.
- Hashing algorithms are utilized like a digital certificate in cryptographic applications.
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.
Few basic differences are as follows:
- 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!)
- In the Map, the original order of elements is preserved. This is not true in case of objects.
- The Map is an instance of an object but the vice-versa is not true.
On the other hand, the Map has only one way of creation.
let map = new Map();//Empty
console.log(map);
let map = new Map([[1, "Sam"], [2, "John"]]); // 1-> Sam, 2->John
console.log(map);
Output:
Map(0)
Map(2)
- Accessing Element:
- Map uses its inbuilt get() method for accessing its elements.
map.get(1);
- Object simply uses the ‘key’ name with a dot operator to access its elements.
obj.id; obj[id];
- Map uses its inbuilt get() method for accessing its elements.
- Check if a key already exists:
- Map uses it’s inbuilt has() function for this.
map.has(1);//returns boolean value true or false.
- Object uses ‘===’ operator for performing the task.
var doExist = obj.1 === undefined; //returns boolean value.
- Map uses it’s inbuilt has() function for this.
- Adding new Element:
- Map uses set() method to add new element.
map.set(4, 5); //{5->4, 7->9, 4->5}
- In object, it is done directly.
obj["Demo"]="Map vs Object"; //{1->Object Name, 2->Test, Demo->Map vs Object}
- Map uses set() method to add new element.
- Getting the size:
- Map automatically updates its size and get the easiest.
console.log(map.size);
- In object, the size needs to be calculated manually with the help object.keys().
console.log(object.keys(obj).length);
- Map automatically updates its size and get the easiest.
- Objects are a great choice for situations where we need simple structure to store data and the type of keys needed is either an integer, strings or symbols.
- Scenarios which needs the application of separate logic to individual property element, the object is definitely the choice.
- JSON gives direct support for object but not with map(yet).
- Map is completely hash whereas Object is more than that.
Comments
Post a Comment