Repeated membership checks: list search vs associative map
A repeated-lookup experiment comparing linear list search with a prebuilt associative membership map.
Question and test setup
When a fixed collection is queried many times, how large is the timing difference between array_search and isset on a keyed map?
The keyed map should make repeated lookups substantially cheaper, at the cost of building and retaining an additional structure.
Permission lists, cache-key allowlists and deduplication pipelines often perform many membership tests against one stable collection.
The map approach is not free: construction time and memory matter when the collection is used only once or changes constantly.
The measured gap
What this run shows: Associative map lookup was cheaper for repeated membership checks after its structure existed; map-construction cost belongs in the end-to-end decision. The slowest-to-fastest median spread was 43350.46% on the measured host.
| Variant | Median | Middle 50% | Operations/s | Additional measure |
|---|---|---|---|---|
| Associative map lookupFastest | 38.36 µs | 38.22 µs–39.25 µs | 26,065 | — |
| Linear array_search | 16.67 ms | 14.04 ms–16.92 ms | 60 | — |
What the numbers suggest
This study demonstrates an algorithmic difference rather than a minor syntax choice. Repeated linear scans grow with both collection size and query count, while keyed lookup shifts work into construction and memory.
For a one-off lookup, converting the entire list may be wasteful. For hundreds of lookups against a stable list, the map is usually the more predictable design.
When the difference is worth acting on
- Build a keyed membership map when the same collection will be queried repeatedly.
- Include map-construction time in short-lived or single-use workloads.
- Use array_key_exists instead of isset only when stored null values must count as present.
Repetition and environment
- The fixture contains 5,000 unique string keys and 1,200 deterministic lookup requests.
- The linear variant uses strict array_search; the map variant uses isset on a prebuilt keyed set.
- Map construction is intentionally outside the timed lookup loop and is discussed separately.
Open measured environment
- Measurement run
- 2026-07-22T15:23:56+00:00
- PHP
- 8.4.23 (cli)
- Operating system
- Linux 4.18.0-553.85.1.el8_10.x86_64
- Architecture
- x86_64
- Processor
- AMD EPYC-Rome Processor
- Reported host memory
- 15.02 GiB host total
- CLI OPcache
- 0
- PHP memory limit
- 1024M
- Loaded study extensions
- json, openssl, zlib, mbstring, pdo_sqlite
- Timing clock
- hrtime(true)
- Measured rounds
- 7
Reasons the ranking may move
- Memory cost of the keyed map is not reported in the timing table.
- Key distribution and collection size can change absolute results.
Primary documentation
These sources describe the PHP functions under test. The performance ranking comes only from this published fixture and environment.