Skip to content

Instantly share code, notes, and snippets.

@fernandocarletti
Created January 20, 2014 19:40
Show Gist options
  • Save fernandocarletti/8527567 to your computer and use it in GitHub Desktop.
Save fernandocarletti/8527567 to your computer and use it in GitHub Desktop.
PHP json_decode array vs. object benchmark
<?php
$iterations = 1000000;
$initialTime = microtime(true);
$jsonString = <<<JSON
{
"Response":
{
"MetaInfo":
{
"Timestamp":"2014-01-20T19:28:58.495+0000",
"NextPageInformation":"2"
},
"View":[
{
"_type":"SearchResultsViewType",
"ViewId":0,
"Result":[
{
"Relevance":1,
"Distance":0.1,
"MatchLevel":"houseNumber",
"MatchQuality":
{
"Country":1,
"State":1,
"County":1,
"City":1,
"District":1,
"Street":[
1
],
"HouseNumber":1,
"PostalCode":1
},
"MatchType":"pointAddress",
"Location":
{
"LocationId":"LINK_931447247_R_PA_31617980",
"LocationType":"point",
"DisplayPosition":
{
"Latitude":52.53086,
"Longitude":13.38469
},
"NavigationPosition":[
{
"Latitude":52.53098,
"Longitude":13.38458
}
],
"MapView":
{
"TopLeft":
{
"Latitude":52.5319842,
"Longitude":13.3828421
},
"BottomRight":
{
"Latitude":52.5297358,
"Longitude":13.3865379
}
},
"Address":
{
"Label":"Invalidenstraße 116, 10115 Berlin, Deutschland",
"Country":"DEU",
"State":"Berlin",
"County":"Berlin",
"City":"Berlin",
"District":"Mitte",
"Street":"Invalidenstraße",
"HouseNumber":"116",
"PostalCode":"10115",
"AdditionalData":[
{
"value":"Deutschland",
"key":"CountryName"
},
{
"value":"Berlin",
"key":"StateName"
}
]
},
"MapReference":
{
"ReferenceId":"931447247",
"MapVersion":"Q4/2013",
"Spot":0.31,
"SideOfStreet":"right",
"CountryId":"20147700",
"StateId":"20187401",
"CountyId":"20187402",
"CityId":"20187403",
"DistrictId":"20187417"
}
}
}
]
}
]
}
}
JSON;
for ($i = 0; $i < $iterations; $i++) {
json_decode($jsonString);
}
echo "Object: " . (microtime(true) - $initialTime) . "s" . PHP_EOL;
$initialTime = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
json_decode($jsonString, true);
}
echo "Array: " . (microtime(true) - $initialTime) . "s" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment