Skip to Content
Learning JavaScript Data Structures and Algorithms - Third Edition
book

Learning JavaScript Data Structures and Algorithms - Third Edition

by Loiane Avancini
April 2018
Beginner to intermediate content levelBeginner to intermediate
426 pages
10h 19m
English
Packt Publishing
Content preview from Learning JavaScript Data Structures and Algorithms - Third Edition

Creating better hash functions

The lose-lose hash function we implemented is not a good hash function, as we have concluded (too many collisions). We would have multiple collisions if we used this function. A good hash function is composed of certain factors: the time to insert and retrieve an element (performance) and also a low probability of collisions. We can find several different implementations on the internet, or we can create our own.

Another simple hash function that we can implement and which is better than the lose-lose hash function is djb2, which is as follows:

djb2HashCode(key) {  const tableKey = this.toStrFn(key); // {1}  let hash = 5381; // {2}  for (let i = 0; i < tableKey.length; i++) { // {3} hash = (hash * 33) + tableKey.charCodeAt(i); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning JavaScript Data Structures and Algorithms

Learning JavaScript Data Structures and Algorithms

Loiane Avancini

Publisher Resources

ISBN: 9781788623872Supplemental Content