In daily work, we need to store some structural data in the database. There are two common methods: serialize and JSON format.

Both have their own advantages and disadvantages, such as serialization supporting object format, serialized data will save data type and number. JSON format is shorter than serialized data, and it is more applicable when front-end and back-end interact.

But in the actual project, which format should be used to store data? There are different opinions on the internet, and veterans prefer serialization (probably because JSON format was introduced after PHP 5.2), while younger guys prefer JSON (maybe they haven’t heard of serialization). Today, apart from the rest, we can only see which format performs better in terms of performance.

testing environment

Test Date: 23 July 2016

Tester: MacBook Pro (13 inches, early 2015)

CPU:2.7 GHz Intel Core i5

Memory: 8 GB 1867 MHz DDR3

PHP version: PHP 5.6.15

Test instructions

Test steps:
1. Serialization and JSON compression of small data
2. Serialization and JSON decompression of small data
3. Serialization and JSON compression of large data
4. Serialization and JSON decompression of large data
Test methods:
Each test cycle 10 times, each operation cycle 10,000 times

Start testing

The test conclusion is at the bottom, the test code is at the bottom, and the partners who are not interested in the data can pull directly to the bottom.

Small Data Compression

test data

[1,2,3,4,5,6,7,8,9,0]

serialize

0.013802766799927
0.012701034545898
0.015209913253784
0.012706995010376
0.012805938720703
0.013393878936768
0.013147830963135
0.012894153594971
0.013478994369507
0.012289047241211

json

0.0054340362548828
0.0052530765533447
0.0050327777862549
0.0051569938659668
0.0053970813751221
0.0050327777862549
0.0049920082092285
0.0052528381347656
0.0051090717315674
0.0069460868835449

Small Data Decompression

test data

Serialization: a:10:{i:0; i:1; i:1; i:2; i:2; i:3; i:3; i:4; i:4; i:5; i:5; i:6; i:6; i:7; i:7; i:8; i:9; i:9; i:0;}
JSON:[1,2,3,4,5,6,7,8,9,0]

serialize

0.012141942977905
0.012423992156982
0.011405944824219
0.011841058731079
0.011488914489746
0.013920068740845
0.011885166168213
0.01164984703064
0.011460065841675
0.012734889984131

json

0.014571905136108
0.014070987701416
0.015500068664551
0.013802051544189
0.015894174575806
0.014295101165771
0.013956069946289
0.013836145401001
0.014487028121948
0.012983083724976

Large Data Compression

test data

Afraid of the impact of typesetting, only put on the Internet address, you can copy and view
http://api.mooninbox.com/api4/?api=goods/all

serialize


0.9694550037384
0.98343706130981
0.97917294502258
1.0035130977631
0.9904420375824
1.0137100219727
0.9673900604248
1.0110490322113
0.96677398681641
0.98873591423035

json

1.7659771442413
1.8020439147949
1.7754938602448
1.7524900436401
1.7396509647369
1.716423034668
1.708487033844
1.7435081005096
1.7137379646301
1.724583864212    

Large Data Decompression

test data

Afraid of the impact of typesetting, only put on the Internet address, you can copy and view
http://api.mooninbox.com/api4/?api=goods/all

serialize


1.0362830162048
1.0082030296326
1.0194120407104
1.0099349021912
1.0123431682587
1.0072951316833
1.0213930606842
1.0140759944916
1.0116169452667
1.0359060764313

json

4.1445369720459
4.1114408969879
4.0740749835968
4.1012029647827
4.1110889911652
4.0948538780212
4.1167409420013
4.0959279537201
3.9271330833435
3.935909986496        

test result

In small data compression, JSON is nearly twice as fast as serialization, while in other aspects serialization is faster than json, especially in large data decompression, which is four times faster.

With this data support, what would you choose?

Anyway, I will use json, and after getting json, I will throw it directly to the front end or mobile end, let them deal with it by themselves. The reason is that “the performance of the server is very valuable, ha ha ha”

Test code


<?php
function ser($data)
{
    $i = 0;
    $ret = '';
    while($i < 10000) {
        $ret = serialize($data);
        $i ++;
    }
    return $ret;
}
function unser($data)
{
    $i = 0;
    $ret = '';
    while($i < 10000) {
        $ret = unserialize($data);
        $i ++;
    }
    return $ret;
}
function json($data)
{
    $i = 0;
    $ret = '';
    while($i < 10000) {
        $ret = json_encode($data);
        $i ++;
    }
    return $ret;
}
function unjson($data)
{
    $i = 0;
    $ret = '';
    while($i < 10000) {
        $ret = json_decode($data,true);
        $i ++;
    }
    return $ret;
}
$short = [1,2,3,4,5,6,7,8,9,0];
set_time_limit(0);
$i = 0;
while($i<10) {
    $start = microtime(true);

    $ret = ser($arr);
    $end = microtime(true);


    echo $end - $start;
    echo '</br>';
    $i++;
}