O(1) — constant complexity

O(1) — constant complexity

What does it mean? The algorithm always takes the same number of operations, no matter how much data there is. This is gold. It's what you want to see in critical places: permission checks, cache access, reading a value from an associative array.

Example: looking up an element in a hash table

$array = ['a' => 1, 'b' => 2, 'c' => 3];
$value = $array['b'];

In this example we find the value by its key right away. The execution time doesn't depend on the size of $array — 3 elements or 3000. No loops or recursion — that's exactly O(1).

This type of complexity shows up in PHP and other technologies more often than you might think — and the better you recognise it, the more precisely you can write optimal code. This is how Redis storage, cookies, LocalStorage and framework caches work, for example.

Next up — O(n), the classic linear complexity you've definitely seen and write every day in every loop.

Whole series · Next: O(n) — linear complexity →


First published in Ukrainian in the Telegram channel “Created to die” on June 20, 2025.