Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of the efficiency of JS Array in memory

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article shares with you the content of an example analysis of the efficiency of JS arrays in memory. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

JS array

The structure of the JS array as we know it is similar to that of other languages, that is, the physical memory is continuous, so the less the number of moves of the array members, the more efficient it is, and the array is generally allocated to a contiguous piece of memory, like this.

You may wonder why you want to draw a border. In fact, our array defaults to its length when it is defined, that is, when we want to add elements to the array, we need to change its length.

Efficiency comparison

JS provides several api to make it easy to add elements to arrays, such as push,unshift

Push () is to add an element at the end of the array. Here we don't need to manipulate other elements, we just need this,length+=1.

Unshift () adds an element to the head of the array, and all we see is that as long as the head of the surface array adds an element, all the members of the underlying array need to move backward, in the order from tail to head, as shown below.

Give me a chestnut.

Let's write a chestnut to clearly compare the time spent between the two. Our unshift took 0.24ms and push took 0.8ms, although the time spent increases exponentially based on the number of elements moved.

Function _ shift () {var arr = [] console.time ('_ shift') for (let I = 0; I

< 1000; i++) { arr.unshift(1) } console.timeEnd('_shift') } _shift()function _push() { var arr = [] console.time('_push') for (let i = 0; i < 1000; i++) { arr.push(1) } console.timeEnd('_push') } _push()

Discontiguous memory problem

In addition, we all know that JS arrays can store data of different data types in the same array, so whether the array of JS allocates continuous memory depends on the type of members of our array. When the members of the array are of the same class, it is continuous memory. When the members of the array have arrays and strings, then discontinuous memory will be allocated, and multiple discontiguous memories will form a linked list. When the order of magnitude of stored data is large, it will cause the problem of inefficiency. Let's make a time-consuming comparison between the same array members and different array members. In order to highlight the effect, I increased the number of times to the seventh power of 10.

Function _ diff () {var arr = new Array (10000000) arr.push ({name: "cxy"}) console.time ('_ diff') for (let I = 0; I < 100000000; iTunes +) {arr [I] = I} console.timeEnd ('_ diff')} _ diff () function _ same () {var arr = new Array (10000000) console.time ('_ same') for (let I = 0 I < 100000000; iTunes +) {arr [I] = I} console.timeEnd ('_ same')} _ same ()

Finally, the difference between the two can be clearly compared, and the operation of an array of discontiguous memory is several times longer than that of an array of contiguous memory.

Thank you for reading! This is the end of the article on "example analysis of the efficiency of JS arrays in memory". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report