Junior — Middle
How is the access time to an array element by index determined?
sobes.tech AI
Answer from AI
Access time to an array element by index is usually considered constant — O(1). This is because arrays in memory are stored as a contiguous block, and the address of an element is calculated using the formula:
start_address + index * element_size
In JavaScript (Node.js), arrays are implemented as objects with additional optimization, but index access remains very fast and close to constant time.
Example:
const arr = [10, 20, 30, 40];
console.log(arr[2]); // 30
Here, access to arr[2] occurs directly without iterating over elements.