How JavaScript uses hashing
![]() |
https://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/CPT-Hashing-Password-Hashed.svg/712px-CPT-Hashing-Password-Hashed.svg.png |
This blog post serves to help shed some light on how JavaScript uses hashing but first we'll have to cover the basic terms such as hashing, hash tables, and understanding how it differs from encryption.
What is hashing?
Hashing is a method used to alter values to conceal the original raw values. It involves applying a hash algorithm or function - a mathematical operation - to a value and getting a hash value as a result. This is done to secure passwords stored in databases so that they're not stored as simple plain text. Hashing is also used in version control as a hash algorithm always produces the same digest when given the same input. So digests of files can be compared to determine if they have been altered or not.
Hash Tables
Hash tables are store key-value pairs created by hashing algorithms that can receive a number or string and create an integer. Hash tables are another use for hashing and are beneficial as data structures for finding data a lot quicker with a complexity of O(1) - which is even more efficient than a binary search operation.
Encryption VS Hashing
The difference between hashing and encryption are:
- With encryption, a function is used that formats a value to a changed value but can be decrypted - returning the original value.
- Hashing is different because once a value has been changed its original format can't be retrieved.
Hashing in JavaScript
In JavaScript, the Map object is used in hashing. A Map object is an entity/collection that holds key-value pairs and every time these pairs are inserted, the Map object keeps track of the insertion order. Map objects are implemented in the hash table and in other operations of the complexity of O(1). Traditionally objects have been used to do the job maps do however offering a complexity of O(n) which is less efficient than a map. More reseasons why Map objects are more preferable to objects are:
- Keys in a traditional object are ordered however the order is complex especially as the collection gets bigger. Maps however offer a simple method to ordering keys and are kept in the order of insertion.
- Collisions are more prone to happen when using objects due to the default keys it creates that might collide with the ones one might create when adding data to the object. Whereas Map objects don't contain any default keys on creation.
- Keys in an object must be strings or symbols however Map objects provide better flexibility with keys given the liberty to be primitives (such as strings, numbers, booleans, etc.), functions, or objects.
- Map objects can be directly iterated but objects don't contain any default manner of doing this.
- With objects, it is difficult to determine the number of items in an object as it has to be done manually whereas with Map objects the size property can be used to determine this.
To conclude I hope this was helpful in helping you understand the hashing and how you can apply Map objects to your JavaScript projects. Happy Coding <😉 />
References
Anon., 2015. ECMAScript®
2015 Language Specification. [Online]
Available at: https://262.ecma-international.org/6.0/#sec-map-objects
[Accessed 22 June 2021].
Baum, J., 2020. Stop Using Objects as Hash Maps in
JavaScript. [Online]
Available at: https://betterprogramming.pub/stop-using-objects-as-hash-maps-in-javascript-9a272e85f6a8
[Accessed 22 June 2021].
Hyperion Dev., 2021. Hashing, s.l.: s.n.
Mozilla, 2021. Map. [Online]
Available at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
[Accessed 22 June 2021].
Wikimedia, 2012. File:CPT-Hashing-Password-Hashed.svg. [Online]
Available at: https://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/CPT-Hashing-Password-Hashed.svg/712px-CPT-Hashing-Password-Hashed.svg.png
[Accessed 22 June 2021].
Comments
Post a Comment