serialize() vs. var_export() vs. json_encode()

Permanent Link: serialize() vs. var_export() vs. json_encode() 16. November 2009 RSS Feed for comments on RSS-Feed für Kommentare zu: serialize() vs. var_export() vs. json_encode() comments feed

There are times when you need to store an array, for example when your array is an index you wanna use again the next time you run your application. In order to store an array you have to transform it into some kind of string represantation first, most people would probably use serialize(). But there are also 2 other ways to achieve that: var_export() and json_encode().

After having them stored the functions to interpret the strings as arrays would be unserialize() if you use serialize(), eval() if you use var_export() and json_decode() if you use json_encode().

So, what about the performance?

In order to test that I wrote a little profiling script that first created some random array with 1.000.000 elements, then exported the array and then imported it again. For the json_encode() test the script looked like that:

$array = array_fill(0, 1000000, rand(1, 9999));

$start = microtime(true);
$export = json_encode($array);
$end = microtime(true);
$duration = $end - $start;
print('JSON Encode: ' . $duration . PHP_EOL);

$start = microtime(true);
$import = json_decode($export);
$end = microtime(true);
$duration = $end - $start;
print('JSON Decode: ' . $duration . PHP_EOL);

Apart from the exporting and importing functions used, the script for serialize() and var_export() looked pretty much the sime, var_export() being the only exception, since I had to add an ending ; to $export in order for it to work with eval().

While it is understandable that importing takes longer for every method than importing, the differences in time are quite astounding:

It's not only interesting to see that unserialize() is damn slow but also that JSON is fastest, which also gets quite clear when looking at combined results:

 

Since it's still in the Ubuntu repositories, I did the performance tests with PHP 5.2.6

6 comments

Séverins Gravatar

Séverin
16.11.2009, 12:30 o'clock

Be carefull, your tests are not complete enough.
I tested something else on my side : small arrays.

# $array = array_fill(0, 20, rand(1, 9999));
#
# $start = microtime(true);
# for ($i=0; $i<1000000; $i++) {
# $export = json_encode($array);
# }
# $end = microtime(true);
# $duration = $end - $start;
# print('JSON Encode: ' . $duration . PHP_EOL);
#
# $start = microtime(true);
# for ($i=0; $i<1000000; $i++) {
# $import = json_decode($export);
# }
# $end = microtime(true);
# $duration = $end - $start;
# print('JSON Decode: ' . $duration . PHP_EOL);

The results are quite differents :

* Serialize encode : 12.25s
* Serialize decode : 6.64s
* Total : 18.89s

* Json encode : 4.85s
* Json decode : 12.67s
* Total : 17.52s

I didn't try the var_export + eval case.

With large arrays, I found the same results than you, but with small arrays, the difference isn't so big. And with a single entry array, serialize become faster.


And, what happen with …

> Large array containing objects : JSon is faster
> Large array containing small arrays: JSon is faster+
> Large array containing large arrays: JSon is faster++

> Small array containing objects : Serialize is faster+
> Small array containing small arrays : No differences
> Small array containing large arrays : Serialize is faster++

> String or Integer : Serialize is faster+++++++


My conclusions :
If you know the exact format of the data to store, use the best function (see above).
If you are sure to store arrays but you don't know its size or content, use Json.
If you have generic function to store datas that can be of any type including basic types such as string or integer, use serialize.

Dominik Jungowskis Gravatar

Dominik Jungowski
16.11.2009, 14:22 o'clock

That's quite interesting. Somehow I should have done that. If I have the time later I will benchmark all 3 possibilities with different array sizes and write a new blogpost about it

Jakob Ketterls Gravatar

Jakob Ketterl
17.11.2009, 16:49 o'clock

there's another downside to the json approach: according to the documentation, the data that is to be encoded, needs to be in the utf-8 charset. i've tried ignoring that and ended up with corrupted data in my app. so if your data contains any special chars, you might stick with one of the slower method.

p.s: yes one could use utf8_encode to fix that, but i do believe that that would make it too slow. i haven't benchmarked it though.

Séverins Gravatar

Séverin
17.11.2009, 18:29 o'clock

And there's another thing to check : the memory used by each method.

EllisGLs Gravatar

EllisGL
17.11.2009, 18:49 o'clock

I rewrote a small caching class (JG_Cache) to use JSON, gave it a test.. Definately faster with serialize.

Dominik Jungowskis Gravatar

Dominik Jungowski
17.11.2009, 19:05 o'clock

I just did a new benchmark with different array sizes and a fourth method - see latest blogpost.

As for the memory usage: I have thought about that aswell, but I had no time to benchmark that now. It would be nice to know nevertheless

Write a comment

(will not be published)