Hướng dẫn hash string javascript
Many of the answers here are the same Show
Here's one I did—cyrb53, a simple but high quality 53-bit hash. It's quite fast, provides very good* hash distribution, and because it outputs 53 bits, has significantly lower collision rates compared to any 32-bit hash. Also, you can ignore SA's CC license as it's public domain on my GitHub.
*It is roughly similar to the well-known MurmurHash/xxHash algorithms. It uses a combination of multiplication and Xorshift to generate the hash, but not as thorough. As a result it's faster than either would be in JavaScript and significantly simpler to implement, but may not pass all tests in SMHasher. This is not a cryptographic hash function, so don't use this for security purposes. Like any proper hash, it has an avalanche effect, which basically means small changes in the input have big changes in the output making the resulting hash appear more 'random':
You can optionally supply a seed (unsigned integer, 32-bit max) for alternate streams of the same input:
Technically, it is a 64-bit hash, that is, two uncorrelated 32-bit hashes computed in parallel, but JavaScript is limited to 53-bit integers. If convenient, the full 64-bit output can be used by altering the return statement with a hex string or array.
Be aware that constructing hex strings drastically slows down batch processing. The array is much more efficient, but obviously requires two checks instead of one. I also included Just for fun, here's TinySimpleHash, the smallest hash I could come up with that's still decent. It's a 32-bit hash in 89 chars with better quality randomness than even FNV or DJB2: Hash Tables are a data structure that allow you to create a list of paired values. You can then retrieve a certain value by using the key for that value, which you put into the table beforehand. Nội dung chính
A Hash Table transforms a key into an integer index using a hash function, and the index will decide where to store the key/value pair in memory: Hash table for storing phone books (from Wikipedia)You'll commonly use a Hash Table because of its fast search, insertion, and delete operations:
Source from Wikipedia This tutorial will help you understand Hash Table implementation in JavaScript as well as how you can build your own Hash Table class. First, let's look at JavaScript's How to Use Hash Tables with Object and Map Classes in JavaScriptThe most common example of a Hash Table in JavaScript is the In the following example, the key JavaScript object is an example of Hash Table implementationBut JavaScript's
For example, the JavaScript object inherited method call exampleJavaScript doesn't block an attempt to overwrite the
JavaScript object inherited property gets overwrittenTo handle these shortcomings, JavaScript created another implementation of the Hash Table data structure which is called Just like JavaScript Map class is another implementation of Hash TableUnlike the You also can't overwrite Map type property can't be overwrittenAs you can see from the code above, you can't add a new entry to the The Iterating through a Map objectNow that you've learned how JavaScript implements Hash Tables in the form of How to Implement a Hash Table Data Structure in JavaScriptAlthough JavaScript already has two Hash Table implementations, writing your own Hash Table implementation is one of the most common JavaScript interview questions. You can implement a Hash Table in JavaScript in three steps:
Alright, let's start with creating the HashTable class initial propertiesAll your key/value pairs will be stored inside the How to write the hash() methodNext, you need to create the A simple way to create the hash would be to sum the ASCII code of the characters in the key using the
But since the To ensure that the hash value doesn't exceed the bucket size, you need to use the modulo operator as shown below:
Now that you have the How to write the set() methodTo set the key/value
pair in your Hash Table, you need to write a
Now that the How to write the get() methodTo get a certain value from the Hash Table, you need to write a
This way, the So far so good. Let's add another method to delete key/value pair from the Hash Table next. How to write the remove() methodTo delete a key/value pair from the Hash Table, you need to write a
With that, you now have a working How to Test the Hash Table ImplementationIt's time to test the Hash Table implementation. Here's the full code for the Hash Table implementation again: The HashTable
implementation in JavaScriptTo test the Testing HashTable set() methodThen, let's try to retrieve them using the Testing HashTable get() methodFinally, let's try to delete one of these values with the
Testing HashTable remove() methodAlright, all the methods are working as expected. Let's try another insertion with a new Hash Table index collision Oops! Looks like we got into some trouble here. 😨 How to Handle Index CollisionSometimes, the hash function in a Hash Table may return the same The same Right now, the data stored in our Hash Table implementation looks as follows:
To handle the
To create the second array, you
need to update the
The complete
Next, update the
Finally, you need to update the
With that,
your As a bonus, let's add a
Here's the complete Complete
HashTable class implementationYou can test the implementation by creating a new Another HashTable testNow there's no collision inside the ConclusionIn this tutorial, you've learned what a Hash Table is and how JavaScript uses it to create the You've also learned how to implement your own By using a Hash Table data structure, you will be able to create an associative array with fast search, insertion, and delete operations. 😉 Thanks for reading this tutorialIf you want to learn more about JavaScript, you may want to check out my site at sebhastian.com, where I have published over 100 tutorials about programming with JavaScript, all using easy-to-understand explanations and code examples. The tutorials include String manipulation, Date manipulation, Array and Object methods, JavaScript algorithm solutions, and many more. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started |