Home >Web Front-end >JS Tutorial >Fast cloning of JavaScript arrays (slice() function) and array sorting, shuffling and searching (sort() function)_Basic knowledge

Fast cloning of JavaScript arrays (slice() function) and array sorting, shuffling and searching (sort() function)_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 19:22:091022browse

Variables in JavaScript are operated by reference, and the same is true for arrays.
I wanted to copy an array two days ago, but I have been struggling to find a way (I will never use the traversal method)

I accidentally picked up the "JavaScript Definitive Guide" and flipped through the array. The operation function found the slice() function.

slice() was originally used to intercept a part of the array. Here I use it to copy the array. Its format is as follows:
array.slice(start, end)
If end is omitted parameter, the split array contains all elements from start to the end of the array.

Now to use it to copy the array, just one line, haha:
var newArray=oldArray.slice(0);



All these functions, use one sort() can be done.

1. Sorting:
The default sort() sorts by character encoding:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]
Now let’s It is sorted by numerical value:

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
Just pass a comparison function to Just sort. If the value of the comparison function is less than 0, it means that a must appear before b, otherwise after b.
2. Out of order:
Just let the comparison function return -1 or 1 randomly:
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute ]

3. Search :
Using sort() to search is quite new, haha, let’s see how it works.
[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute ]
In this way, all the elements containing the letter 'a' in the array are placed in front of the array, and after some simple processing, the search results can be output.
Using slice to copy arrays is indeed a good method, and it is actually used more often. However, it is difficult to say whether the efficiency is better than traversal, because none of us know how js slice is implemented. , but presumably it should be more efficient, and more importantly, it is much simpler to write.

The second sorting method is really clever, so you don’t have to write the shuffling algorithm yourself ^^

As for the third one. . . If you don’t use regular rules and must use Sort, it will still be much simpler to write, but the efficiency may not be higher than traversal. You must know that traversal does not mean low efficiency. The traversal algorithm for linear list search is O(N) complexity. Yes, I guess JS's sort uses the q-sort algorithm (it would be too shabby if you use bubbles ^^), so it is at least O(N*LogN) complexity (sorting is more complicated than searching and is also easier. Understand), that is to say, using Sort matching is basically faster than using direct traversal in terms of execution efficiency.
I am used to using concat for array copy. For example: newArr = oldArr.concat(), the efficiency is the same as slice(0).

I have tested that copying arrays with slice or concat is much faster than traversing.
During the testing process, I also discovered another feature. When an array is retrieved through subscripts, it seems to be retrieved through a method similar to traversal. For example, if the array arr has 1 million array elements, then compared with these two expressions: i = arr[0] and i = arr[999999], the former will get the value faster! (If you don’t believe it, you can make up a relevant example and try it yourself). That is to say, when traversing the array, the time required for each value will increase as the subscript value increases.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn