Get the App
SLTechnology News&Howtos  ›  Development  › 

Where to start the subscript of javascript array

Shulou Source: shulou.com Published: 2022-06-02 21:40:25 10月20日 Update

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!

Tags: Array subscript address reason byte subtraction instruction physics language learning operation formula memory content foundation efficiency data data structure structure offset Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information Huawei Shulou Tech Info Xiaomi