Data formatsDecision report7 repeated rounds

JSON vs PHP serialization for an internal round trip

Timing and output-size comparison for JSON and native PHP serialization of the same nested data.

Decision brief

The choice this benchmark informs

How do JSON and PHP serialize/unserialize compare when a PHP-only process stores and reloads a nested payload?

Cache payloads are often encoded without documenting whether they must survive language changes, be inspected by operators or cross a trust boundary.

Native unserialize has security implications for untrusted input, especially when object instantiation is permitted. This experiment disables classes and does not endorse decoding attacker-controlled payloads.

Result and trade-off

Lowest measured medianPHP serialize round trip
Median time425.05 µs
Median spread76.58%

What this run shows: PHP serialize round trip produced the lower round-trip median in this PHP-only fixture. Interoperability and long-term storage compatibility can reverse the recommendation. The slowest-to-fastest median spread was 76.58% on the measured host.

Measured results. Lower median time is faster.
VariantMedianMiddle 50%Operations/sAdditional measure
PHP serialize round tripFastest425.05 µs420.63 µs–429.56 µs2,353106,020 output bytes
JSON round trip750.56 µs743.85 µs–861.24 µs1,33267,457 output bytes

Timing and extra output metrics are calculated directly from the downloadable samples.

How to interpret the difference

A PHP-only cache can legitimately prioritize local speed, but portability and operational transparency are long-lived architectural properties.

Use the measured result only after deciding whether non-PHP consumers, manual inspection, schema evolution or untrusted input are relevant.

Decision rules

  • Use JSON for interoperable storage and external interfaces.
  • Keep native serialization limited to trusted, PHP-controlled data paths.
  • Version cache payloads so encoding changes can be rolled out safely.

Fixture and measurement design

Pre-run hypothesis

Native serialization may be competitive for PHP-only data, while JSON offers stronger interoperability and inspectability.

  1. Both variants encode and then decode the same 400-record nested array.
  2. The JSON path uses explicit exception handling. The native path disables allowed classes during unserialize.
  3. Output byte length is recorded for the encoded form.
55operations/sample
7measured rounds
2variants
Medianprimary statistic
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

  • Objects, references and custom serialization hooks are not tested.
  • Compression and persistent cache-network latency are excluded.

Primary documentation

These primary sources define the formats and APIs involved. They should be read alongside the measured host data, not replaced by it.

Use the data in another environment