Some of you might know the problem: You have to store a dynamic array of values in your database. But what’s the fastest solution on the php side? What’s better for the database? Any more pros and cons? I did a bit of testing and came to conclusion that json is the way to go, if you have to store an array as value to your database.
I tested both with an multidimensional array consisting of 3 layers and 5 values on each layer. The last layer has random strings that have 6 chars each. I repeated the test 50 times. Here are my results:
The serialize() function needed about 76% longer than json_encode(). While serialize() needed around 0.064 ms, json_encode() did it in only 0.036 ms.
The unserialize() function was 21% faster than json_decode(). While unserialize() needed only 0.067 ms, json_decode() did need 0.085ms. So the serialize functions seem to need around the same time to serialize/unserialize, but jsons encoding seems to be a bit faster than the decoding.
But what about the size of the serialized / encoded arrays? I think this is one of the most important things, as the processing only takes milliseconds. In this case json is the big winner. The serialized array string was 12.123 chars long and the json string only 5.878. If you really have to store an large amount of data, you should go with json. Especially if you want to take care of your database size.