Home Web Front-end JS Tutorial JS performance analysis of adding items to array_javascript skills

JS performance analysis of adding items to array_javascript skills

May 16, 2016 pm 04:13 PM
js performance array

Compared the performance between 4 ways to add items to an array:

Use indexer to add

Copy code The 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 push method

Copy code The 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 concat method

Copy code The 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 code The 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 10,000 (ten thousand) times:

Copy code The code is as follows:

index: 0.310ms
push: 1.476ms
concat: 8.911ms
concat with array: 2.261ms

Set times to 100000 (one hundred thousand) times:

Copy code The code is as follows:

index: 1.967ms
push: 11.980ms
concat: 70.410ms
concat with array: 28.292ms

Set times to 1000000 (millions) times:

Copy code The code is as follows:

index: 138.559ms
push: 93.074ms
concat: 608.768ms
concat with array: 243.371ms

Set times to 10000000 (ten million) times:

Copy code The code is as follows:

index: 1473.733ms
push: 611.636ms
concat: 6058.528ms
concat with array: 2431.689ms

Summary

This conclusion is only applicable to Chrome browser

The execution efficiency of the concat method is the slowest
Compared with the parameter passing of the two concat methods, when the parameters are accepted as arrays, the execution efficiency is higher than when the parameters are accepted as non-arrays
In most cases, the execution efficiency of the indexer is higher than that of the push method
When the number of executions increases, the execution efficiency of the indexer begins to be inferior to the push method

Browser comparison

Thanks to the netizen for pointing out that I lack experience, so I will add a horizontal comparison between browsers here

The first is to use the concat method. In IE and Firefox, if the parameter is an array, the execution efficiency is slower than if the parameter is a non-array, but the difference is not big
Then the index and push methods are definitely faster than concat. Using the index method in IE is always faster than push. In Firefox, push is slightly better but the difference is not big
Comparing the execution efficiency of the index and push methods between the three browsers, the difference is huge. The execution efficiency of Firefox is much higher than that of IE and Chrome. In the case of millions of times, it is basically 10 times faster. Compared with other browsers, the execution efficiency of Firefox is basically 10 times faster. The slowest of the two

The following are the results of millions of times:

Copy code The code is as follows:

// firefox
index: timer started
index: 229.79ms
push: timer started
push: 205.12ms
concat: timer started
concat: 2136.99ms
concat with array: timer started
concat with array: 2365.18ms
```

Copy code The code is as follows:

//ie
index: 2,533.744 milliseconds
push: 3,865.979 milliseconds
concat: 4,303.139 milliseconds
concat with array: 4,792.208 milliseconds

This article only discusses the performance of JS, and deepens friends' understanding of javascript through comparison. I hope you will like it.

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Performance comparison of different Java frameworks Performance comparison of different Java frameworks Jun 05, 2024 pm 07:14 PM

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

How to optimize the performance of multi-threaded programs in C++? How to optimize the performance of multi-threaded programs in C++? Jun 05, 2024 pm 02:04 PM

Effective techniques for optimizing C++ multi-threaded performance include limiting the number of threads to avoid resource contention. Use lightweight mutex locks to reduce contention. Optimize the scope of the lock and minimize the waiting time. Use lock-free data structures to improve concurrency. Avoid busy waiting and notify threads of resource availability through events.

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

Performance comparison of Java frameworks Performance comparison of Java frameworks Jun 04, 2024 pm 03:56 PM

According to benchmarks, for small, high-performance applications, Quarkus (fast startup, low memory) or Micronaut (TechEmpower excellent) are ideal choices. SpringBoot is suitable for large, full-stack applications, but has slightly slower startup times and memory usage.

Can arrays be used as function parameters? Can arrays be used as function parameters? Jun 04, 2024 pm 04:30 PM

Yes, in many programming languages, arrays can be used as function parameters, and the function will perform operations on the data stored in it. For example, the printArray function in C++ can print the elements in an array, while the printArray function in Python can traverse the array and print its elements. Modifications made to the array by these functions are also reflected in the original array in the calling function.

How good is the performance of random number generators in Golang? How good is the performance of random number generators in Golang? Jun 01, 2024 pm 09:15 PM

The best way to generate random numbers in Go depends on the level of security required by your application. Low security: Use the math/rand package to generate pseudo-random numbers, suitable for most applications. High security: Use the crypto/rand package to generate cryptographically secure random bytes, suitable for applications that require stronger randomness.

See all articles