Home Web Front-end JS Tutorial Comparative analysis of performance of Array's push and unshift methods_javascript skills

Comparative analysis of performance of Array's push and unshift methods_javascript skills

May 16, 2016 pm 06:09 PM
array push

From the principle, we can know that the efficiency of unshift is low. The reason is that every time it adds an element, it moves the existing element down one position. But how big is the efficiency difference? Let’s test it out below.
The main hardware of the test environment: CPU T7100 (1.8G); memory 4G DDR2 667; hard disk 5400 rpm. Main software: The operating system is Windows 7; the browser is Firefox 3.6.9. Test code:

Copy code The code is as follows:

var arr = [ ], s = new Date;
// push performance test
for (var i = 0; i < 50000; i ) {
 arr.push(i);
}
console.log( new Date - s);
s = new Date;
arr = [ ];
// unshift performance test
for (var i = 0; i < 50000; i ) {
arr.unshift(i);
}
console.log( new Date - s);

This code performs push and unshift operations 50,000 times respectively. After running once, The result is:
12
1152
It can be seen that unshift is almost 100 times slower than push! Therefore, you should still use unshift with caution, especially for large arrays. So if we must achieve the unshift effect, are there any other methods? The answer is yes.
Array has a method called reverse, which can reverse an array. First use push to add the elements to be put into the array, and then perform reverse again to achieve the unshift effect. For example:
Copy code The code is as follows:

for (var i = 0; i < 50000; i ) {
 arr.push(i);
}
arr.reverse();

What about the performance of reverse? Let’s test it again:
Copy code The code is as follows:

var arr = [ ], s = new Date;
for (var i = 0; i < 50000; i ) {
 arr.push(i);
}
arr.reverse();
console.log( new Date - s) ;

The result is:
12
It can be seen that the reverse performance is extremely high, and there is no additional consumption, so you can use it with confidence.
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Sort array using Array.Sort function in C#

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

Simple and clear method to use PHP array_merge_recursive() function

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

How to use the array_combine function in PHP to combine two arrays into an associative array

Detailed explanation of PHP array_fill() function usage Detailed explanation of PHP array_fill() function usage Jun 27, 2023 am 08:42 AM

Detailed explanation of PHP array_fill() function usage

How to use the Array module in Python How to use the Array module in Python May 01, 2023 am 09:13 AM

How to use the Array module in Python

What are the common causes of ArrayStoreException in Java? What are the common causes of ArrayStoreException in Java? Jun 25, 2023 am 09:48 AM

What are the common causes of ArrayStoreException in Java?

Introduction to how to use the PHP array_change_key_case() function Introduction to how to use the PHP array_change_key_case() function Jun 27, 2023 am 10:43 AM

Introduction to how to use the PHP array_change_key_case() function

Introduction to the usage of PHP array_flip() function Introduction to the usage of PHP array_flip() function Jun 27, 2023 am 08:24 AM

Introduction to the usage of PHP array_flip() function

See all articles