JSON decoding into arrays or objects in PHP
A same-payload comparison of associative-array and stdClass decoding paths in PHP.
The choice this benchmark informs
Does choosing associative arrays instead of objects materially change the cost of decoding a medium JSON document?
Many PHP codebases choose json_decode(..., true) by habit. Others retain objects to mirror the source document or pass DTO-like structures deeper into the application.
Changing representation can affect every later access, validation and transformation step, so this report treats decode speed as one input rather than a universal rule.
Result and trade-off
What this run shows: Associative arrays led this isolated decode run, but representation choice still changes every downstream access and validation step. The slowest-to-fastest median spread was 6.8% on the measured host.
| Variant | Median | Middle 50% | Operations/s | Additional measure |
|---|---|---|---|---|
| Associative arraysFastest | 562.06 µs | 544.18 µs–576.99 µs | 1,779 | — |
| Objects | 600.28 µs | 584.36 µs–604.45 µs | 1,666 | — |
Timing and extra output metrics are calculated directly from the downloadable samples.
How to interpret the difference
A narrow benchmark can identify decode overhead, but it cannot determine the best domain representation. Arrays are often convenient for transformation and framework interoperability; objects can communicate a different intent and avoid string-key syntax at call sites.
Treat the measured winner as a local baseline. A representation change is justified only when profiling shows JSON decoding is a meaningful part of request time and the change does not weaken correctness.
Decision rules
- Choose the representation that matches the surrounding API and validation model.
- Profile end-to-end request handling before optimizing only json_decode.
- Keep JSON_THROW_ON_ERROR or equivalent explicit error handling in production code.
Fixture and measurement design
The two representations may have a measurable timing difference, but downstream access style, type expectations and maintainability are likely to matter more than a small isolated gap.
- The exact compact JSON fixture used by both variants contains 400 nested records.
- Each operation decodes the full document and reads the same record identifier to ensure the decoded structure is consumed.
- No schema hydration, validation or application mapping is included.
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
Where this result stops
- The study does not include typed object hydration, serializers or framework normalizers.
- Memory retention after decoding is not isolated in this timing-only experiment.
Primary documentation
These primary sources define the formats and APIs involved. They should be read alongside the measured host data, not replaced by it.