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

Where to start the subscript of javascript array

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "javascript array subscript where to start," interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's take you to learn "javascript array subscript where to start"!

Javascript array subscripts start at 0. Reasons: 1. Starting from 0, you can reduce a subtraction operation, reduce CPU instruction operations, and improve CPU efficiency;2. The address of physical memory starts from 0.

Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.

Javascript array subscripts start at 0.

So why do javascript array subscripts start at 0 instead of 1?

Reason 1: Historical reasons

Languages appear in order from early to late C, Java, JavaScript.

C array subscripts start at 0->Java also->JavaScript also.

Reduce additional learning and understanding costs.

Reason 2: Reduce CPU instruction operations

(1) Subscripts start from 0:

Array addressing--arr[i] = base_address + i * type_size (1)

Where base_address is the first address of array arr, arr0 is the array with offset 0, that is, the first address of array arr;i is the offset, type_size is the number of bytes of array type, for example, int is 32 bits, that is, 4 bytes.

(2) Subscripts start with 1:

Array addressing--arr[i] = base_address +(i -1)* type_size (2)

Comparing the two calculation formulas, we can find that formula (2) requires one more i-1 operation per CPU address, that is, one more subtraction instruction operation.

Arrays, the basic data structure, are frequently used indirectly (as a container base data structure, such as Java's ArrayList) or directly in any high-level programming language, so they consume as little CPU resources as possible. Starting from 0, one subtraction operation can be reduced, improving CPU efficiency.

Reason 3: Physical memory addresses start at 0.

Computer main memory is an array of consecutive byte units, each byte corresponding to a unique physical address, the first byte address is 0.

At this point, I believe that we have a deeper understanding of "javascript array subscript from where to start," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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