JS performance analysis of adding items to array_javascript skills
Compared the performance between 4 ways to add items to an array:
Use indexer to add
console.time("index");
var a = [];
for (var i = 0, l = times; i < l; i ) {
a[i] = i;
}
console.timeEnd("index");
Use push method
console.time("push");
var a = [];
for (var i = 0, l = times; i < l; i ) {
a.push(i);
}
console.timeEnd("push");
Use concat method
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
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:
index: 0.310ms
push: 1.476ms
concat: 8.911ms
concat with array: 2.261ms
Set times to 100000 (one hundred thousand) times:
index: 1.967ms
push: 11.980ms
concat: 70.410ms
concat with array: 28.292ms
Set times to 1000000 (millions) times:
index: 138.559ms
push: 93.074ms
concat: 608.768ms
concat with array: 243.371ms
Set times to 10000000 (ten million) times:
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:
// 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
```
//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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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.

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.

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.

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.

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.

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.
