serialize() vs. var_export() vs. json_encode() Part 2
17. November 2009
comments feed
Based on the comments from my first benchmark in the serialize() vs. var_export() vs. json_encode article 2 days ago, I decided to benchmark once again, this time using different array sizes and I also added the JSON method with recursive UTF8 encoding beforehand. This time, all results are the combined results (exporting and importing), the testing script was exactly the same (except JSON+UTF8 which also had a utf8_encode_recursive function). Let's see what happens when using array sizes from 10 to 1000 elements:

So far, json_encode itself is fastest, closely followed by serialization. JSON with UTF8 Encoding is slowest, but consider that all the values are all below 0,02 seconds.
So far so good, let's look at the results with arrays from 10.000 to 1.000.000 elements:

Apart from the exception Serialization the values mostly develop like with smaller arrays. What's quite odd is that Serialization's runtime seems to be exploding when stepping up from 100.000 to 1.000.000. Why that is so, I cannot tell. Although JSON with UTF8 Encoding is slower than var_export and JSON itself, it's still way faster than serialization.
Conclusions
- None of the methods scales linear.
- For smaller arrays, JSON is the way to go, as long as your data is already UTF8 encoded. If not, you might want to take serialization.
- With larger arrays JSON is still fastest as long as your data is already UTF8 encoded, otherwise var_export is the best choice.
Closing, let's take a look at the graphs from 10 - 1.000.000 item large arrays:

2 comments
Jaimie Sirovich
20.11.2009, 23:28 o'clock
The other thing you might want to look into is the performance with respect to recursion. I've noticed that the 3 methods vary a lot with nesting.
Unserialize seems to shine on recursive data. JSON decode does a lot worse. And for various reasons (both space and time?), the eval method does the worst.
If you're dealing with read-biased data that's recursive, serialize is best … I think. But YMMV. Test it.
recent posts
Jakob Ketterl
18.11.2009, 12:33 o'clock
i wouldn't say that none of these scales linear. it does look so in your graphs, but your x axis is not properly scaled - so it's hard to tell.