JS adds performance analysis to the array, and js array adds Performance

Source: Internet
Author: User

JS adds performance analysis to the array, and js array adds Performance

Compared the performance between four methods that can add items to an array:

Add With Indexer

Copy codeThe Code is as follows:
Console. time ("index ");
Var a = [];
For (var I = 0, l = times; I <l; I ++ ){
A [I] = I;
}
Console. timeEnd ("index ");

Use the push Method

Copy codeThe Code is as follows:
Console. time ("push ");
Var a = [];
For (var I = 0, l = times; I <l; I ++ ){
A. push (I );
}
Console. timeEnd ("push ");

Use the concat Method

Copy codeThe Code is as follows:
Console. time ("concat ");
Var a = [];
For (var I = 0, l = times; I <l; I ++ ){
A. concat (I );
}
Console. timeEnd ("concat ");

Use the concat method. The parameter is an array.

Copy codeThe Code is as follows:
Console. time ("concat with array ");
Var a = [];
For (var I = 0, l = times; I <l; I ++ ){
A. concat ([I]);
}
Console. timeEnd ("concat with array ");

Set times to 10000 () times:

Copy codeThe Code is as follows:
Index: 0.310 ms
Push: 1.476 ms
Concat: 8.911 ms
Concat with array: 2.261 ms

Set times to 100000 (100,000) times:

Copy codeThe Code is as follows:
Index: 1.967 ms
Push: 11.980 ms
Concat: 70.410 ms
Concat with array: 28.292 ms

Set times to 1000000 (million) times:

Copy codeThe Code is as follows:
Index: 138.559 ms
Push: 93.074 ms
Concat: 608.768 ms
Concat with array: 243.371 ms

Set times to 10000000 (10 million) times:

Copy codeThe Code is as follows:
Index: 1473.733 ms
Push: 611.636 ms
Concat: 6058.528 ms
Concat with array: 2431.689 ms

Summary

This conclusion is only applicable to the chrome browser.

Concat method execution efficiency is the slowest
Compared with the passing parameters of the two concat methods, when the accepted parameter is an array, the execution efficiency is higher than that of the accepted parameter.
In most cases, the execution efficiency of the indexer is higher than that of the push method.
As the number of executions increases, the efficiency of the indexer execution is not as high as that of the push method.

Browser comparison

Thanks to some netizens for pointing out that I am not experienced enough. Here I will add a horizontal comparison between browsers.

First, the concat method is used. In ie and firefox, the parameter is the array execution efficiency, but other parameters are not the array, but the difference is not great.
Then, the index and push methods are faster than concat. in ie, the index method is always faster than push. In firefox, the push method is slightly better, but the difference is not big.
Comparing the execution efficiency of the index and push methods between the three browsers is huge. The execution efficiency of firefox is much higher than that of ie and chrome. In the case of millions of times, basically 10 times faster, and ie is the slowest

The following are the results of millions of requests:

Copy codeThe Code is as follows:
// Firefox
Index: timer started
Index: 229.79 ms
Push: timer started
Push: 205.12 ms
Concat: timer started
Concat: 2136.99 ms
Concat with array: timer started
Concat with array: 2365.18 ms
'''

Copy codeThe Code is as follows:
// Ie
Index: 2,533.744 Ms
Push: 3,865.979 Ms
Concat: 4,303.139 Ms
Concat with array: 4,792.208 Ms

This article only discusses the performance of javascript. Through comparison, I hope you will enjoy it.

Related Article

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.