Friday, May 28, 2010

Preferred method to store PHP arrays (json_encode vs serialize)

I need to store a multi-dimensional associative array of data in a flat file for caching purposes. I might occasionally come across the need to convert it to JSON for use in my web app but the vast majority of the time I will be using the array directly in PHP.

Would it be more efficient to store the array as JSON or as a PHP serialized array in this text file? I've looked around and it seems that in the newest versions of PHP (5.3), json_decode is actually faster than unserialize.

I'm currently leaning towards storing the array as JSON as I feel its easier to read by a human if necessary, it can be used in both PHP and JavaScript with very little effort, and from what I've read, it might even be faster to decode (not sure about encoding, though).

Does anyone know of any pitfalls? Anyone have good benchmarks to show the performance benefits of either method?

Thanks in advance for any assistance.
php performance arrays json serialization
flag

asked Apr 29 '09 at 20:09
KyleFarris
2,184412


8 Answers
oldest newest votes
17


Depends on your priorities.

If performance is you absolute driving characteristic, then by all means use the fastest one. Just make sure you have a full understanding of the differences before you make a choice

* JSON converts UTF-8 characters to unicode escape sequences. serialize() does not.
* JSON will have no memory of what the object's original class was (they are always restored as instances of stdClass).
* You can't leverage __sleep() and __wakeup() with JSON
* Only public properties are serialized with JSON
* JSON is more portable

And there's probably a few other differences I can't think of at the moment.
EDIT

A simple speed test to compare the two

<?php

ini_set( 'display_errors', 1 );
error_reporting( E_ALL );

//  Make a bit, honkin test array
//  You may need to adjust this depth to avoid memory limit errors
$testArray = fillArray( 0, 5 );

//  Time json encoding
$start = microtime( true );
json_encode( $testArray );
$jsonTime = microtime( true ) - $start;
echo "JSON encoded in $jsonTime seconds
";

//  Time serialization
$start = microtime( true );
serialize( $testArray );
$serializeTime = microtime( true ) - $start;
echo "PHP serialized in $serializeTime seconds
";

//  Compare them
if ( $jsonTime < $serializeTime )
{
    echo "json_encode() was roughly " . number_format( ($serializeTime / $jsonTime - 1 ) * 100, 2 ) . "% faster than serialize()";
}
else if ( $serializeTime < $jsonTime )
{
    echo "serialize() was roughly " . number_format( ($jsonTime / $serializeTime - 1 ) * 100, 2 ) . "% faster than json_encode()";
} else {
    echo 'Unpossible!';
}

function fillArray( $depth, $max )
{
    static $seed;
    if ( is_null( $seed ) )
    {
        $seed = array( 'a', 2, 'c', 4, 'e', 6, 'g', 8, 'i', 10 );
    }
    if ( $depth < $max )
    {
        $node = array();
        foreach ( $seed as $key )
        {
                $node[$key] = fillArray( $depth + 1, $max );
        }
        return $node;
    }
    return 'empty';
}
?>

link|flag

edited Apr 29 '09 at 21:30


answered Apr 29 '09 at 20:21
Peter Bailey
20.1k11643


You make some great points. Fortunately, for my case, I'm storing simple arrays (of other arrays, ints, bools, and strings) no objects. If I were storing objects, IMO, serialize would be the way to go. – KyleFarris Apr 29 '09 at 20:32
1

Excellent work dude. This will benefit everyone. I ran the test about 30 times and json_encode won every single time with around 100% (average) performance increase over serialize. I added json_decode and unserialize tests and json_decode won everytime in about 10 tests with an average performance benefit of ~20% over unserialize. Thanks for this. – KyleFarris Apr 30 '09 at 13:25

Too bad json_encode/json_decode is php 5.2 and above only. disgusts_uncover_akin_umbriel – dreftymac Feb 23 at 1:28

12


JSON is simpler and faster than PHP's serialization format and should be used unless:

* You're storing deeply nested arrays: json_decode(): "This function will return false if the JSON encoded data is deeper than 127 elements."
* You're storing objects that need to be unserialized as the correct class
* You're interacting with old PHP versions that don't support json_decode

link|flag

answered Apr 29 '09 at 20:13
Greg
48.1k45284


Great answer. Haha, 127 levels deep seems a bit insane; thankfully I'm only going like 2-3 at the most. Do you have any data to back up the fact that json_decode/json_encode is faster than unserialize/serialize? – KyleFarris Apr 29 '09 at 20:34

I did test it a while ago and json came out faster - I don't have the data any more though. – Greg Apr 29 '09 at 21:02

@Kyle - I added a speed test to my answer. On my server, json_encode() is averaging about 100% faster that serialize() – Peter Bailey Apr 29 '09 at 21:32
5


I've written a blogpost about this subject: "Cache a large array: JSON, serialize or var_export?". In this post it is shown that serialize is the best choice for small to large sized arrays. For very large arrays (> 70MB) JSON is the better choice.
link|flag

answered Jul 7 '09 at 14:20
Takkie
5111


Very cool man. Thanks. ;-) – KyleFarris Jul 8 '09 at 21:07
2


If you are caching information that you will ultimately want to "include" at a later point in time, you may want to try using var_export. That way you only take the hit in the "serialize" and not in the "unserialize".
link|flag

answered Apr 29 '09 at 23:04
Jordan S. Jones
3,192513


2


I augmented the test to include unserialization performance. Here are the numbers I got.

Serialize

JSON encoded in 2.5738489627838 seconds
PHP serialized in 5.2861361503601 seconds
Serialize: json_encode() was roughly 105.38% faster than serialize()


Unserialize

JSON decode in 10.915472984314 seconds
PHP unserialized in 7.6223039627075 seconds
Unserialize: unserialize() was roughly 43.20% faster than json_decode()

So json seems to be faster for encoding but slow in decoding. So it could depend upon your application and what you expect to do the most.
link|flag

answered Nov 23 '09 at 19:14
Jeff Whiting
211


0


This is really awsome. and I think json_encode /json_decode is absouletly superb for me!
link|flag

answered Oct 21 '09 at 3:08
arunoda
3


0


Before you make your final decision, be aware that the JSON format is not safe for associative arrays - json_decode() will return them as objects instead:
<?php
$config = array(
    'Frodo'   => 'hobbit',
    'Gimli'   => 'dwarf',
    'Gandalf' => 'wizard',
    );
print_r($config);
print_r(json_decode(json_encode($config)));
?>
Output is:
<?php
Array
(
    [Frodo] => hobbit
    [Gimli] => dwarf
    [Gandalf] => wizard
)
stdClass Object
(
    [Frodo] => hobbit
    [Gimli] => dwarf
    [Gandalf] => wizard
)
?>
link|flag

answered Oct 21 '09 at 3:34
too much php
8,63431938


Indeed, you are right. I mean, it is Javascript object notation afterall! Thankfully, if you know that what you encoded using json_encode was an associative array, you can easily force it back into an array like so: $json = json_encode($some_assoc_array); $back_to_array = (array)json_decode($json); Also it's good to note that you can access objects the same way as arrays in PHP so in a typical scenario, one wouldn't even know the difference. Good point though! – KyleFarris Dec 7 '09 at 21:10
1

@toomuchphp, sorry but you are wrong. There is a second parameter for json_decode 'bool $assoc = false' that makes json_decode produce an array. @KyleFarris, this should also be faster than using the typecast to array. – Jan P. Jan 20 at 14:30

@Jan thanks for the correction – too much php Jan 21 at 1:31
0


Thanks all for comparing two method :)

usually, i use json just for ajax, and serialize for storing in database .... regards

No comments: