Fast cloning of JavaScript arrays (slice () function) and array sorting, ordering, and searching (sort () function) _ Basics

Source: Internet
Author: User
Tags arrays
Operations on variables in JavaScript are referred to by reference, and the same is the case for arrays.
Two days ago want to copy an array, has been struggling to find a way (traversal of the method I was absolutely not used)

Accidentally picked up the "JavaScript Authority Guide" turned over the array operation function, found the slice () function.

Slice () is used to intercept a portion of an array, which I use to copy the array, which is formatted as follows:
Array.slice (start, end)
If the end argument is omitted, the Shard array contains all the elements starting at start and ending with the array.

Now you want to use it to copy the array, on one line, hehe:
var newarray=oldarray.slice (0);



All of these functions can be done with a sort ().

1. Sort:
The default sort () is sorted by character encoding:
<script type= "Text/javascript" > <!--var testarray=[3,324,5345,6546,134,5654,665]; Testarray.sort (); alert (Testarray); --> </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

now you want to sort it by numeric size:
<script type= "Text/javascript" > <!--var testarray=[3,324,5345,6546,134,5654,665]; Testarray.sort (function (a,b) {return a-b;}); alert (Testarray); --> </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Just pass a comparison function to sort, and if the value of the comparison function is less than 0, it means that a must appear in front of B, otherwise it will be behind B.
2. Disorderly Order:
Let the comparison function pass back randomly to 1 or 1:
<script type= "Text/javascript" > <!--var testarray=[1,2,3,4,5,6,7,8,9,10,22,33,55,77,88,99]; Testarray.sort (function () {return math.random () >0.5?-1:1;}); alert (Testarray); --> </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

3. Search:
Use sort () to play the search is quite fresh, oh, see how to play it.
Now I'm going to find out all the elements in the array that contain the letter ' a ', and if there's no sort (), it seems to be just traversal, the efficiency of traversal ... That's called Horror!! The following are specific practices:
<script type= "Text/javascript" > <!--var testarray=[' df ', ' RTR ', ' wy ', ' dafd ', ' Dfs ', ' Wefa ', ' Tyr ', ' Rty ', ' Rty ', ' ryt ', ' AfDs ', ' wer ', ' te '; Testarray.sort (function (a,b) {return A.indexof (' a ') ==-1?1:-1;}); alert (Testarray); --> </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

This puts the elements of the array containing the letter ' a ' in front of the array, and after some simple processing, you can output the search results.
It's a good way to copy arrays with slice. And actually use will be more, but the efficiency and traversal is difficult to say, because we do not know how JS slice is achieved, but presumably should be more efficient, more importantly, write more simple.

The second sort of method is really clever, so do not have to write their own shuffle algorithm ^ ^

As for the third one ... If you do not have a regular, you must use the sort, the same is more simple to write, but the efficiency may not be higher than the traversal, to know that traversal does not mean inefficient, linear table one-way search traversal algorithm is O (N) complexity, JS sort I guess use is q-sort algorithm (if with bubble then it is too shabby bar ^ ^), so at least O (n*logn) complexity (sorting is easier to understand than search complexity), which means that the execution efficiency with sort matching is almost certainly not as fast as using direct traversal.
Array copy I used to use concat. such as: NEWARR = Oldarr.concat (), Efficiency and slice (0).

I have tested that slice or concat copy arrays are much faster than traversal.
I also found another feature in the testing process, when an array was evaluated by the subscript, it seemed to be the same as the traversal method. For example, the array arr has 1 million array elements, then the two expressions: i = arr[0] and i = arr[999999] compared to the former will be faster to the value! (If you do not believe you can make a relevant example to try). That is, when the array is traversed, the time required for each value increases as the subscript value increases.

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.