PHP serialize/unserialize is faster than json_encode/json_decode

One of the things that I tend to focus on with a website is how quickly everything loads and executes. However, that focus can sometimes get to be a bit too narrow, only considering the performance of those resources that are required for the initial page load, and not for other dynamic aspects of the site. We recently implemented New Relic on one site, and gained much insight into how long each aspect of our site took to load, and how long each of the most popular requests took to execute.

Surprisingly, one particular request that we use to update the pricing without accessing the database was taking as long as other requests that had to access the database multiple times to complete the request. At that point, I got curious and began timing each line of code to see just how long each operation took to execute. The first thing I found was that it took about 75% of the total time to open the stored json file and use json_decode to parse it into a PHP object. As a result, I started looking at other methods of persisting data to disk, and stumbled across the serialize/deserialize combination and that it may take a bit longer to serialize the data, but the desearialize function should be much faster. After testing, this seemed to take what previously was a 100ms operation down to around 50ms.

Luckily, that was not the end of the options for optimizing the execution of this request. It turned out that many of the requests did not need the entire payload of the data that was in the serialized file and only delivering the needed data could reduce the download size by almost half. Interestingly enough, when we made this change, the download time for this request did not change by 50%, but the real change was on the server side. Instead of deserialize for the entire dataset, we limited the data in the file that we serialize and deserialize, making that process faster, and it turns out that it took the average time required down to about 25ms.

If you have the correct tooling, you really can make some significant performance improvements without changing the logical design of your application.

Related Posts

May 1, 2014
One minute

Why Use AddThis.com Sharing Buttons

AddThis.com produces a bit of JavaScript that enables website owners to quickly and easily add social sharing buttons/links to their website. In addition to allowing users to easily share your content socially, they also provide analytics information about what content is shared via which method, giving a greater insight into visitor behavior.

However, just because it provides these benefits to websites does not necessarily mean that it is a good tool to implement on your website. If you are concerned about the performance and the total payload size for your website, then you may have some reservations about the AddThis sharing buttons. On one site I was looking at, when all of the AddThis JavaScript, CSS and Images were combined, it took up 250kB. When you consider that this is a responsive website, and many mobile users only get 300MB of data before they get to overage fees, you hope that there are other ways to implement the sharing to the major social sites.

Jul 7, 2014
2 minutes

AddThis Can Cause Your Site To Not Load

Over the last few days, I have run across quite a few websites that seem to never finish loading. After waiting for 20 seconds or more, I give up, realizing that whatever content was on the site wasn’t worth it anymore for me to stare at a blank white browser until it loaded however much longer. Unfortunately for those websites, they are losing traffic that they will never get back.

Apr 25, 2014
4 minutes

Optimizing Website Load Time

Assuming you have already done a few things to improve the page load time of your website, such as using a Varnish caching server, GZipping your content in transit, minifying that same content, and turning on all caching options that Magento or your web platform of choice have available, there is still more you can do. When it comes to website performance, the 80/20 rule definitely applies. 80% of the performance tweaks that you perform will only provide a miniscule improvement to the site load time, while the 20% of things you do make a big difference. Any time I am looking to speed of the performance of a website, I seek those 20% items that give you the biggest bang for your buck.