Detailed introduction to how JavaScript implements hash tables
This article brings you relevant knowledge about javascript, which mainly introduces related issues about how JavaScript implements hash tables, and encapsulates the entire structure of the array into which the final data is inserted. , what you get is the hash table, I hope it will be helpful to everyone.
Related recommendations: javascript learning tutorial
Hash tables are usually implemented based on arrays. But it has many advantages over arrays:
- It can provide very fast insert-deletion-find operations
- No matter how much data, insertion and deletion need to be close to constant time: that is, O(1) time level. In fact, it only takes a few machine instructions to do it.
- The hash table is faster than the tree, and you can basically find the desired element instantly
- The hash table is much easier to code than the tree
Some shortcomings of hash tables compared to arrays:
- The data in the hash table is not in order, so it cannot be traversed in a fixed way. The elements
- Normally, the keys in the hash table are not allowed to be repeated, and the same key cannot be placed. It is used to save different elements
- The space utilization is not high, and the bottom layer uses is an array, and some cells are not utilized
What is a hash table?
- Hash tables are not easy to understand, unlike arrays, linked lists, and trees, which can express their structure and principles in the form of graphics.
- The structure of the hash table is an array, but its magic lies in a transformation of the subscript value, which we can call Hash function, HashCode can be obtained through the hash function.
Some concepts of hash tables
- Hashing: Convert large numbers The process of forming a subscript within the array range is called hashing;
- Hash function: We usually use Word is converted into big number, and the code implementation of big number for hashing is placed in a function, which is called Hash function;
- Hash table: Encapsulate the entire structure of the array inserted into the final data, and get is the hash table.
Problems that still need to be solved:
- The hashed subscript is still possibleDuplicate, how to solve this What's the problem? This situation is called conflict. Conflict is inevitable, and we can onlysolve the conflict.
Methods to resolve conflicts
Two common solutions to resolve conflicts:
- Option 1:Chain address Method(zipper method);
As shown in the figure below, we will perform the remainder operation on 10 for each number, then the remainder The range 0~9 is used as the subscript value of the array. Moreover, the position corresponding to each subscript value in the array no longer stores a number, but stores an array or linked list## composed of numbers that have the same remainder after a remainder operation. #.
Summary: The way to resolve conflicts with the chain address method is that the data stored in each array unit is no longer A single data , but a chain. The commonly used data structure for this chain is array or linked list. The search efficiency of the two data structures is equivalent (because the elements of the chain are generally Not too much).
- Option 2:
- Open address method;
Looking for blank cells To place conflicting data items.
- Linear detection
- Secondary detection
- Rehashing method
chain address method.
Excellent hash functionThe advantage of a hash table is its speed, so the hash function cannot use complex algorithms that consume high performance. One way to improve speed is tominimize multiplications and divisions in the hash function.
A high-performance hash function should have the following two advantages:- Fast calculation;
- Uniform distribution;
Quick calculation
Horner's Law: In China, Horner's Law is also called Qin Jiushao's Algorithm. The specific algorithm is:
When finding the value of a polynomial, First, calculate the value of the linear polynomial in the innermost bracket, and then calculate the value of the linear polynomial layer by layer from the inside out. This algorithm converts the value of n degree polynomial f(x) into the value of n degree polynomials.
Before transformation:
- Number of multiplications: n(n 1)/2 times;
- Number of additions: n times;
After transformation:
- Number of multiplications: n times;
- Number of additions: n times;
If big O is used to represent the time complexity, it will directly drop from O(N2) before transformation to O(N).
Uniform distribution
In order to ensure that the data is evenly distributed in the hash table, when we need touse constants, try to use Prime numbers; For example: the length of the hash table, the base of Nth power, etc.
HashMap in Java uses the chain address method, and the hashing method uses the formula: index = HashCode (key) & (Length-1)
That is to say, the data is converted into binary for and operations instead of remainder operation. In this way, the computer directly operates on binary data, which is more efficient. However, JavaScript will have problems when performing and operations called big data, so the remainder operation will still be used when using JavaScript to implement hashing.
function HashTable() { // 存放相关的元素 this.storage = []; // 存了多少数据 this.count = 0; // 用于标记数组中一共存放了多少个元素 this.limit = 7; /* 设计哈希函数 ①将字符串转成比较大的数字 ②将大的数字hashCode压缩到数组范围之内 */ HashTable.prototype.hashFunction = function (str, size) { var hashCode = 0; //秦九韶算法(霍纳算法) // 哈希表的长度、N次幂的底数等尽量选取质数 for (var i = 0; i this.limit * 0.75) { var newLimit = this.limit * 2; var prime = this.getPrime(newLimit); this.resize(prime); } }; // 获取 HashTable.prototype.get = function (key) { var index = this.hashFunction(key, this.limit); var bucket = this.storage[index]; if (bucket == null) return null; for (var i = 0; i 7 && this.count 0 ? false : true; }; // size HashTable.prototype.size = function () { return this.count; }; // toString HashTable.prototype.toString = function () { var str = ''; for (var i = 0; i <p><detailed introduction to how javascript implements hash tables src="https://Detailed%20introduction%20to%20how%20JavaScript%20implements%20hash%20tables.php.cn/upload/article/000/000/067/4ca10f86d3fe737c57c2ff0ae29c077f-3.png" alt="Detailed introduction to how JavaScript implements hash tables"></detailed></p><p>Related recommendations: <a href="https://www.php.cn/course/list/17.html" target="_blank">javascript learning tutorial</a><br></p>
The above is the detailed content of Detailed introduction to how JavaScript implements hash tables. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
