Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
msgpack vs json vs igbinary vs php serialize
* Results
ruby json: 19,777,781 bytes
ruby msgpack: 14,757,439 bytes
php serialize(): 33,777,792 bytes
php igbinary: 18,757,571 bytes
php json_encode: 19,777,781 bytes
msgpack vs json (MRI 1.9.2) speed:
user system total real
msgpack:
0.900000 0.040000 0.940000 ( 1.171771)
json:
1.880000 0.130000 2.010000 ( 2.172833)
* Source
Ruby:
require 'msgpack'
require 'json'
require 'benchmark'
a = (0...1_000_000).group_by { |i| "k-#{i}" }
Benchmark.bm do |x|
x.report("msgpack:") { puts "size=" + MessagePack.pack(a).length.to_s }
x.report("json:") { puts "size=" + a.to_json.length.to_s }
end
PHP:
ini_alter('memory_limit', -1);
$a = array();
for ($i = 0; $i < 1000000; $i++) {
$a["k-$i"] = array($i);
}
echo 'serialize()d size=' . strlen(serialize($a)) . "\n";
echo 'igbinary_serialize()d size=' . strlen(igbinary_serialize($a)) . "\n";
echo 'json_encode()d size=' . strlen(json_encode($a)) . "\n";
@staabm
Copy link

staabm commented Apr 27, 2013

Could you also add php+msgpack?

@parf
Copy link

parf commented Sep 4, 2014

On our data sets msg_pack have 50% overhead over igbinary_serialize

strlen(igbinary_serialize($X->d()))
218869 218,869 0x3,56F5

strlen(msgpack_pack($X->d()))
309118 309,118 0x4,B77E

strlen(json_encode($X->d()))
380856 380,856 0x5,CFB8

data - complex 3-4 level deep hash with strings and bigints

@pekatete
Copy link

pekatete commented Aug 15, 2020

10+ years later, msgpack is over 50% smaller than php serialize (and best in the league too).
PHP Version 7.2.24-0ubuntu0.18.04.6
msgpack version: 2.0.3
igbinary version: 2.0.8
json version: 1.6.0

ini_alter('memory_limit', -1);
$a = array();
for ($i = 0; $i < 1000000; $i++) {
$a["k-$i"] = array($i);
}
echo 'serialize()d size=' . number_format(strlen(serialize($a))) . "\n";
echo 'igbinary_serialize()d size=' . number_format(strlen(igbinary_serialize($a))) . "\n";
echo 'msgpack_pack()d size=' . number_format(strlen(msgpack_pack($a))) . "\n";
echo 'json_encode()d size=' . number_format(strlen(json_encode($a))) . "\n";

Results:
Added elapsed time in microseconds - json serialize is way faster)

serialize()d size=33,777,792 (elapsed time = 0.46339988708496)
igbinary_serialize()d size=18,757,571 (elapsed time = 0.63907718658447)
msgpack_pack()d size=14,757,439 (elapsed time = 0.45030903816223)
json_encode()d size=19,777,781 (elapsed time = 0.15625810623169)

@jorgecc
Copy link

jorgecc commented Dec 9, 2021

I did some benchmark in PHP (I know it is an old gist but it is the first on Google Search)

https://github.com/EFTEC/php-benchmarks#serializations

Conclusion: I am impressed with igbinary. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment