


Efficiency comparison test of JavaScript combination and string concatenation_javascript skills
During the script development process, a large string is often combined and spliced for output according to a certain rule. For example, when writing a script control, the HTML tag output that controls the appearance of the entire control, or when dynamically analyzing and creating HTML tags after obtaining the server-side return value in AJAX, but I will not discuss the specific application of splicing strings here. I just want to Let’s discuss the efficiency of splicing here.
When we write code, we always use the "=" operator when we write code, s = String; this is the most familiar writing method. I don’t know if you have noticed that the capacity of the combined string is When there are tens of K or even hundreds of K, the script execution is very slow and the CPU usage is extremely high, for example:
var str = "01234567891123456789212345678931234567894123456789";
str = "51234567896123456789712345678981234567899123456789/n";
var result = "";
for(var i=0; i<2000; i ) result = str;
With this one-step operation, the resulting string is 200K, it takes 1.1 seconds (this is related to the computer configuration), and the CPU peak value is 100%. (In order to see the effect more intuitively, I made some more loops). It is conceivable that just such a step of operation consumes more than a second of my time. Coupled with the time consumption of other codes, the execution time of the entire script block becomes unbearable. Is there any optimization solution? Is there any other way? The answer is of course yes, otherwise it would be nonsense for me to write this article.
The faster way is to use an array. When splicing in a loop, it is not concatenated into a string. Instead, the string is put into an array, and finally array.join("") is used to get the result. String, code example:
var str = "01234567891123456789212345678931234567894123456789";
str = "51234567896123456789712345678981234567899123456789/n";
var result = "", a = new Array();
for(var i=0; i<2000; i ) a[i] = str;
result = a.join(""); a = null;
You can test the time it takes to assemble a string of the same size. The result I tested here is: <15 milliseconds. Please note that its unit is milliseconds, which means that it is possible to assemble such a string. For a 200K string, the time consumption of the two modes is almost two orders of magnitude. what does that mean? It means that the latter has finished work and returned from lunch, while the former is still doing hard work. I wrote a test page. You can copy the following code and save it as an HTM file and open it on the web page to test the efficiency difference between the two. Anyway, what I tested is that the former takes half a minute to complete. Or it can be done in 0.07 seconds (loop 10,000 times).
Number of string concatenations
Finally, let me say a few words. Will array join be used for string splicing in the future? This depends on your actual needs. For ordinary combinations of a few or K-level bytes, there is no need to use the array method, because opening array variables is also expensive. If there are more than a few K string combinations, the efficiency of the array is high.
IE 6.0:
String splicing method: The spliced large string is 1,010,000 bytes long, and the splicing takes 22,089 milliseconds!
Array assignment join method: The spliced large string is 1,010,000 bytes long, and the splicing takes 218 milliseconds!
Firefox 1.0:
String splicing method: The spliced large string is 1,010,000 bytes long, and the splicing takes 1,044 milliseconds!
Array assignment join method: The spliced large string is 1,010,000 bytes long, and the splicing takes 1,044 milliseconds!
Mozilla 1.7:
String splicing method: The spliced large string is 1,010,000 bytes long, and the splicing takes 1,045 milliseconds!
Array assignment join method: The spliced large string is 1,010,000 bytes long, and the splicing takes 1,044 milliseconds!
Netscape 7.0:
String splicing method: The spliced large string is 1,010,000 bytes long, and the splicing takes 10,273 milliseconds!
Array assignment join method: The spliced large string is 1,010,000 bytes long, and the splicing takes 1,138 milliseconds!
Opera 7.54:
String splicing method: The spliced large string is 1010000 bytes long, and the splicing takes 6968 milliseconds!
Array assignment join method: The concatenated large string is 1,010,000 bytes long, and the concatenation takes 6,922 milliseconds!
The test results of looping 10,000 times show that the efficiency can be greatly improved in IE and Netscape, while in Firefox Mozilla Opera, the two methods take basically the same time. These data are enough to determine that the array join method is better than traditional string splicing.

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

AI Hentai Generator
Generate AI Hentai for free.

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).

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
