Type-Safe PHP Arrays: 4x Speed Boost Without Sacrificing Readability
The sbwerewolf/language-specific library version 10.4.5 achieves a 4x performance improvement over version 8.4.1. Its gap versus wplake/typed (1.2.2) has narrowed to just 2x. The former uses string-based paths; the latter relies on method chaining for navigating nested data structures.
Example using wplake/typed:
use WPLake\Typed\Typed;
$response = file_get_contents(
'https://geocode-maps.yandex.ru/1.x/?apikey=d3893dc1-c136-4084-b9c-4db26b00463e&geocode=Mohammed+Bin+Rashid+Boulevard+1&lang=en_US&format=json'
);
$object = json_decode(json: $response, associative: true, flags: JSON_THROW_ON_ERROR);
$path = 'response.GeoObjectCollection.featureMember.0.GeoObject.metaDataProperty.GeocoderMetaData.Address.formatted';
// value with fallback
$val = Typed::string($object, $path, 'Address not found');
var_dump($val);
/*
string(77)
"1, Mohammed Bin Rashid Boulevard, Downtown Dubai, Dubai, United Arab Emirates"
*/
$isReal = Typed::stringOrNull($object, $path) !== null;
var_dump($isReal);
/* bool(true) */
Equivalent functionality in sbwerewolf/language-specific:
use SbWereWolf\LanguageSpecific\AdvancedArrayFactory;
$response = file_get_contents('https://geocode-maps.yandex.ru/1.x/?apikey=d3893dc1-c136-4084-b9c-4db26b00463e&geocode=Mohammed+Bin+Rashid+Boulevard+1&lang=en_US&format=json');
$object = json_decode(json: $response, associative: true, flags: JSON_THROW_ON_ERROR);
$data = new AdvancedArrayFactory()->makeAdvancedArray($object);
$formatted = $data
->pull("response")
->pull("GeoObjectCollection")
->pull("featureMember")
->pull()
->pull("GeoObject")
->pull("metaDataProperty")
->pull("GeocoderMetaData")
->pull("Address")
->get("formatted")
->default("Address not found");
$val = $formatted->str();
var_dump($val);
/*
string(77)
"1, Mohammed Bin Rashid Boulevard, Downtown Dubai, Dubai, United Arab Emirates"
*/
$isReal = $formatted->isReal();
var_dump($isReal);
/* bool(true) */
Performance Benchmarks
Benchmarks reflect real-world use cases: parsing JSON in happy-path scenarios, handling missing paths, checking element existence, type coercion, and raw value retrieval.
| Use case | 8.4.1 (before) | 10.4.5 (after) | 1.2.2 (wplake/typed) |
|----------|----------------|----------------|------------------------|
| JSON happy path parsing | 29.210 µs/op | 6.467 µs/op | 3.013 µs/op |
| JSON missing path parsing | 18.716 µs/op | 5.987 µs/op | 1.766 µs/op |
| Element existence check | 27.237 µs/op | 6.277 µs/op | 2.863 µs/op |
| Value type coercion | 28.581 µs/op | 6.740 µs/op | 2.966 µs/op |
| Raw value retrieval | 30.395 µs/op | 6.558 µs/op | 3.015 µs/op |
| Extract all scalar values | 5.897 µs/op | 1.878 µs/op | Not supported |
| Extract all arrays | 7.136 µs/op | 2.431 µs/op | Not supported |
New in 10.4.5: bulk extraction of all scalars and arrays — unavailable in wplake/typed.
Key Architectural Changes
- Immutable
CommonValue: The class is now immutable — no mutations allowed after default value assignment. - Deterministic
pull(): When extracting from an array, always returns the first element — eliminating non-determinism. - Simplified API: Auxiliary classes removed; logic streamlined into a single, cohesive flow.
Backward compatibility is formally broken due to the major version jump (8.4.1 → 10.4.5), but the public API remains unchanged. Consumer code requires no modifications.
New Methods & Capabilities
Full chainable method list:
pull(string $key)— extract a nested element by keypull()— extract the first array element (deterministic)get(string $key)— retrieve a value with existence checkdefault(mixed $fallback)— set fallback for missing valuesstr()— cast to stringisReal()— verify value presence
Example of complex usage for processing API responses with unstable structure.
Why It Matters
- 4x speedup vs. baseline; only 2x slower than the current leader.
- Preserves method-chain readability — no brittle string paths.
- Adds unique features: bulk scalar/array collection.
- Immutability and determinism improve reliability and testability.
- Ideal for mid-to-senior engineers: combines type safety with production-grade performance.
— Editorial Team
No comments yet.