


Comparison and test code of several methods of efficiently assembling strings under JS_javascript skills
When using Ajax to submit information, I may often need to assemble some relatively large strings to complete POST submission through XmlHttp. Although submitting such large information may seem inelegant, sometimes we may have to face such a need. So how fast is the accumulation of strings in JavaScript? Let’s do the following experiment first. Accumulate a string of length 30000.
Test code 1 - Time consumption: 14.325 seconds
var str = "";
for (var i = 0; i < 50000; i ) {
str = "xxxxxx";
}
This code takes 14.325 seconds, and the result is not ideal. Now we change the code to the following form:
Test Code 2 - Time taken: 0.359 seconds
var str = "";
for (var i = 0; i < 100; i ) {
var sub = "";
for (var j = 0; j < 500; j ) {
sub = "xxxxxx";
}
str = sub;
}
This code takes 0.359 seconds! Same result, all we do is assemble some smaller strings first and then assemble into larger strings. This approach can effectively reduce the amount of data copied in memory in the later stages of string assembly. After knowing this principle, we can further dismantle the above code for testing. The code below only takes 0.140 seconds.
Test code 3 - Time consumption: 0.140 seconds
var strArray = new Array();
for (var i = 0; i < 100; i ) {
var sub = "";
for (var j = 0 ; j < 500; j ) {
sub = "xxxxxx";
}
strArray.push(sub);
}
str = String.prototype.concat.apply(" ", strArray);
However, the above approach may not be the best! If the information we need to submit is in XML format (in fact, in most cases, we can try to assemble the information to be submitted into XML format), we can also find a more efficient and elegant method - using DOM objects to assemble it for us string. The following paragraph only takes 0.890 seconds to assemble a string with a length of 950015.
Use DOM objects to assemble information - time consumption: 0.890 seconds
var xmlDoc;
if (browserType == BROWSER_IE) {
xmlDoc = new ActiveXObject("Msxml.DOMDocument");
}
else {
xmlDoc = document.createElement("DOM");
}
var root = xmlDoc.createElement("root");
for (var i = 0; i < 50000; i ) {
var node = xmlDoc.createElement("data");
if (browserType == BROWSER_IE) {
node.text = "xxxxxx";
}
else {
node.innerText = "xxxxxx";
}
root.appendChild(node);
}
xmlDoc.appendChild(root);
var str;
if (browserType == BROWSER_IE) {
str = xmlDoc.xml;
}
else {
str = xmlDoc.innerHTML;
}

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



Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Features and Advantages of C Language: Why is it one of the most popular programming languages? As a general-purpose high-level programming language, C language has many unique features and advantages, which is why it has become one of the most popular programming languages. This article will explore the characteristics and advantages of C language, as well as its wide application in various fields. First of all, C language has concise syntax and clear structure. Compared with other programming languages, the syntax of C language is relatively simple and easy to understand and learn. It uses the characteristics of natural language to enable programmers to

C drive space is running out! 5 efficient cleaning methods revealed! In the process of using computers, many users will encounter a situation where the C drive space is running out. Especially after storing or installing a large number of files, the available space of the C drive will decrease rapidly, which will affect the performance and running speed of the computer. At this time, it is very necessary to clean up the C drive. So, how to clean up C drive efficiently? Next, this article will reveal 5 efficient cleaning methods to help you easily solve the problem of C drive space shortage. 1. Clean up temporary files. Temporary files are temporary files generated when the computer is running.

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Python and C++ are two popular programming languages, each with its own advantages and disadvantages. For people who want to learn programming, choosing to learn Python or C++ is often an important decision. This article will explore the learning costs of Python and C++ and discuss which language is more worthy of the time and effort. First, let's start with Python. Python is a high-level, interpreted programming language known for its ease of learning, clear code, and concise syntax. Compared to C++, Python

Functions and features of Go language Go language, also known as Golang, is an open source programming language developed by Google. It was originally designed to improve programming efficiency and maintainability. Since its birth, Go language has shown its unique charm in the field of programming and has received widespread attention and recognition. This article will delve into the functions and features of the Go language and demonstrate its power through specific code examples. Native concurrency support The Go language inherently supports concurrent programming, which is implemented through the goroutine and channel mechanisms.

Title: Efficient Practice Guide for Go Language Encoding Conversion In daily software development, we often encounter the need to convert text in different encodings. As an efficient and modern programming language, Go language provides a rich standard library and built-in functions, making it very simple and efficient to implement text encoding conversion. This article will introduce practical guidelines on how to perform encoding conversion in the Go language and provide specific code examples. 1.UTF-8 encoding and string conversion In Go language, strings use UTF-8 encoding by default

Introduction to JS-Torch JS-Torch is a deep learning JavaScript library whose syntax is very similar to PyTorch. It contains a fully functional tensor object (can be used with tracked gradients), deep learning layers and functions, and an automatic differentiation engine. JS-Torch is suitable for deep learning research in JavaScript and provides many convenient tools and functions to accelerate deep learning development. Image PyTorch is an open source deep learning framework developed and maintained by Meta's research team. It provides a rich set of tools and libraries for building and training neural network models. PyTorch is designed to be simple, flexible and easy to use, and its dynamic computation graph features make
