JS Sketchbooksee JavaScript think ✏️
← back to phase 4

4.2 — Inside an array: memory & O(1)

Yesterday you read scores[2] and the value just… appeared. Here's the question that separates people who use arrays from people who understand them: if the array had two million elements, would scores[1999999] be slower? Intuition says the engine must walk two million cells to get there. Intuition is wrong.

The answer is one line of arithmetic, and it's the reason arrays exist at all: address = start + index × slot size. Today you learn how an array actually sits in memory (lesson 0.4's wall of slots returns), and why that makes every index lookup a single jump.

That kind of cost has a name professionals use: O(1). This is your first piece of real computer science, and interviewers love asking about it.

watch it happen
const scores = [82, 95, 71];

// the engine's arithmetic for scores[2]:
//   address = start + index × slotSize
//           = 5000  +   2   ×    8
//           = 5016  → jump straight there

console.log(scores[2]);
console.log(scores.length);
console.log(scores[9]);

When line 1 runs, the array asks memory for a CONTIGUOUS run of slots — side by side, no gaps, one block. The array itself remembers just ONE number: where the block starts. Here, address 5000.

the memory wall (lesson 0.4) — every slot has an ADDRESS82addr 5000index 095addr 5008index 171addr 5016index 2an array = a CONTIGUOUS run of memory slots — side by side, nogapsconsole(nothing printed yet)
under the hood

The deeper story, with the real names for things — this part is what turns “I saw it” into “I can explain it.”

Big-O notation is nothing more than a label for how cost grows with data size. O(1): flat — index reads, .length, writing a[i].

O(n): grows in step with the element count — anything that must visit or move every element. You now own the two labels that describe 90% of everyday performance conversations (and a healthy share of interview questions).

Honesty note: JavaScript engines are craftier than one diagram — an array of mixed types, or one with holes, makes V8 fall back to slower internal layouts. The contiguous picture is the honest mental model and the practical advice: keep arrays same-typed and gap-free, and the engine keeps the fast math.

Fun fact: hotels run on the same trick. Room 507 isn't found by checking every door — the number IS the address: floor 5, corridor position 07, walk straight there. An array index is a room number for memory; nobody in a hotel ever does an O(n) search for their bed.

your turn

⌨️ a thousand elements, one jump

Prove the O(1) claim with your own hands: build a 1000-element array, then read its two ends — the far end must cost the same one line as the near end.

requirements:

  • Create an empty array big, then use a loop (lesson 2.6 style) to fill it: element at index i holds i * 2, for indexes 0 through 999.
  • Print the FIRST element, then the LAST element — the last one's index must be computed from the array's length, no hand-typed 999.
  • Print the length itself.

when you press RUN, the console must show exactly:

0
1998
1000

✏️ Quick check 1

An array starts at address 3000; each slot is 8 bytes. Type the address of a[3].

✏️ Quick check 2

Reading a[1] costs the same on a 3-element and a 3-million-element array. Type the big-O name for that kind of cost.

✏️ Quick check 3

Type exactly what this prints:

const a = [1, 2];
console.log(a[5]);
teach it back

🗣️ Now teach it back

Explain to a friend how scores[1999999] can be just as fast as scores[2] — walk them through what an array looks like in memory, the address formula, and what O(1) means.

Write it as if your friend is sitting next to you. Saved to your journal — future-you will use these notes to teach others.

a few sentences, minimum — you’ve got this
to remember
An array is a CONTIGUOUS block of equal-sized slots; the array stores where the block starts.
scores[i] = start + i × slotSize → one jump. O(1), constant time — flat cost no matter the size. (Index = distance, hence 0-based.)
.length is stored bookkeeping (also O(1)). Reading past the end: bounds-checked → undefined. The contiguity law is why removing the front will cost O(n) — next lesson’s bill.
next: 4.3 Growing & shrinking →