Difference between new Array () and var arr = [] in javascript, arrayarr
In learning javascript, you may not know how to distinguish Array functions new Array () from var arr = []. Let's take a look at the summary below.
Var arr = []
This is a way to define arrays literally.
Var arr = new Array ()
This is an array generated by calling the array constructor.
The two methods for defining arrays above are the same.
At present, we generally agree with the following:
Use the new Keyword to open a storage address in memory, which is resource-consuming and memory-consuming.
The literal method directly opens a storage field in the memory, which is relatively simple and does not consume resources.
We should not deny the conclusion above, right.
First, in terms of function, var = arr = [] and var arr = new Array () are the same.
Then we use a piece of code to verify it.
Var startTime = new Date (). getTime (); var test1 = []; var test2 = []; for (var I = 0; I <1000000; I ++) {// test1 [I] = {}; test2 [I] = new Object ();} var endTime = new Date (). getTime (); console. log ('output time consumed: ', endTime-startTime );
Each element in the above two arrays test1 and test2 is an array. you can comment out any line of code in sequence. I have tested it more than 10 times.
Result
36 **
36 **
The two above took about 3600 seconds, but they were not far behind.
I run the MacBook Air (13-inch, Mid 2013), processor: 1.3 GHz Intel Core i5, memory: 4 GB 1600 MHz DDR3, JS Runner Tool
So I think the two methods above are not very different.
[] The only difference in syntax from new Array () is that new Array () can directly set the length of the Array.
When constructing an Array, new Array () has the following methods:
Var arr = new Array ();
Var arr = new Array (8 );
Var arr = new Array ("c", "d", "e ");
Literal type
Var d = ["111", "222", "333"];
Therefore, there is no big gap between the two methods in terms of performance. It is just a kind of habit to use.
In the next step, if I use var arr = ['123', '123', '123']; To define an array, didn't the constructor open a storage address in the memory?
Summary of some common methods for Array in js
Var list = new Array () is the code that we often write in js. Today we will summarize the methods of the objects in the Array.
List [0] = 0;
List [1] = 1;
List [2] = 2;
Or declare as follows: var list = [0, 1, 2]
1 shift () t: Delete the first element of the array and return the deleted Value. Here is 0
2 unshift (3, 4): loads the parameter before the array and returns the length of the array. In list: the values are 3, 4, 1, 1, and 2.
3pop (): Delete the last element of the array and return the deleted Value. Here is 2.
4 push (3): load the parameter to the end of the array and return the length of the array. in the List, the values are 0, 1, 2, and 3.
5 concat (3, 4): concatenates two arrays.
6 splice (start, deleteCount, val1, val2,...): Delete the deleteCount item from the start position, and insert val1, val2 ,...
Reverse: returns the reverse order of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. reverse (); // a: [5, 4, 3, 2, 1] B: [5, 4, 3, 2, 1]
Sort (orderfunction): sorts arrays by specified parameters.
Var a = [1, 2, 3, 4, 5];
Var B = a. sort (); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5]
Slice (start, end): returns a new array consisting of items from the original array that specify the start subscript to the end subscript
Var a = [1, 2, 3, 4, 5];
Var B = a. slice (); // a: [, 5] B: [, 5]
Join (separator): A string is set up for the elements of the array. The separator is separator. If it is omitted, a comma is used as the separator by default.
Var a = [1, 2, 3, 4, 5];
Var B = a. join ("|"); // a: [1, 2, 3, 4, 5] B: "1 | 2 | 3 | 4 | 5"