Connection Array, string method summary

Source: Internet
Author: User
an array of connections

1, concat ()

Concat () can create a new array based on all the items in the current array. Specifically, it creates a copy of the current array and then adds the received parameter to the end of the replica, and finally returns the newly built array.
If one or more arrays are passed to the concat () method, the method adds each item in the array to the result array. If the value passed is not an array, the values are simply added to the end of the result array.

var  colors=[' red ', ' yellow ', ' blue ';
var colors2=colors.concat (' Yellow ', [' black ', ' brown ']);

Console.log (colors);//["Red", "yellow", "blue"]
console.log (colors2);//["Red", "yellow", "blue", "Yellow", " Black "," Brown "]

PS: This method does not change an existing array and creates a new array. We want to allocate the memory space for the newly created array, assign a value to it, and have a little resource consumption.

2, [].push.apply

var arr1= [1,2,3]; 

PS: Cleverly applied the features of the Apply method, so that arr1 is still arr1, but memory is rewritten. second, the connection string

character of string:

Strings in ECMAScript are immutable , that is, once a string is created, their values cannot be changed. To change the saved string for a variable, first destroy the original string, and then fill the variable with another string containing the new value, for example:

1, "+" connector

var lang = "Java";
Lang = lang + "Script";

PS: Early browsers did not optimize this operation. Because the string is immutable, this means that you want to create an intermediate string to store the results of the connection . Frequently creating and destroying strings in the background is unusually low in performance.
Procedure: First create a new string that can hold 10 characters, then populate "Java" and "script" in this string, and the last step is to destroy the original string "Java" and "script", because these two strings are useless. However, in a lower version of the browser (such as IE6), string stitching speed is a process that consumes a lot of performance.

2, with the array as the intermediary, using join () to connect the array

var arr=new Array ();
Arr.push (a);
Arr.push (b);
var str=arr.join ("");

PS: In the early browsers, the intermediate strings were not created and destroyed, and in the case of a large number of string connections , this technique has proved far faster than the use of additive methods.

3, concat ()

var str1 = new String ("This is String One");
var str2 = new String ("This is string two");
var str3 = Str1.concat (STR2);

Ps:
Today, the browser's optimization of strings has changed the situation of string-connected. Safari, Opera, Chrome, Firefox, and IE8 all show better performance with the addition operator. However, the IE8 version was not optimized, so the array method is still valid. This does not mean that we have to do browser detection when strings are connected.
The size and number of strings are considered when deciding how to connect.
when the string is relatively small (less than 20 characters) and the number of connections is smaller (less than 1000), all browsers use the addition operator to easily complete the connection in less than 1 milliseconds. when you increase the number or size of strings, performance decreases significantly in IE7. When the string size increases, the addition operator and number composition techniques in Firefox will have a smaller performance difference. When the number of strings increases, the performance difference between the addition operators and the tricks in safari becomes smaller. When you change the number or size of strings, the addition operator in Chrome and Opera keeps the leading edge. Therefore, because the performance is inconsistent in the browser, the choice of technology depends on the actual situation and the face of the browser.
In most cases, the addition operator is preferred, and if the user is primarily using IE6 or 7, and the string size is larger or more numerous, then array technology is well worth it.

In general, if it is a semantic string, you should not use an array, such as:

' Hello, my name is ' + name;  

If the string is "duplicates of similar circumstances," We recommend using array, such as:

var array = []; 
for (i = 0; i < length; i++) { 
array[i] = ' <li> ' + list[i] + ' </li ' >; 
} 
document.getElementById (' somewhere '). InnerHTML = Array.join (' \ n ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.