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.

May 6, 2014
One minute

Unintended Consequences with Magento Observers on Sales Orders

Anyone that uses Magento to place orders will be hard-pressed to consider this process a speedy one. While it takes a while to process the order under the best of circumstances, there are a few things that you can do that actually make it worse.

One of those things that can make it worse is creating an observer that runs in the middle of the saving of the order processes that is always slow-running, or continues to get slower over time as the data that the Magento site grows. While the observer may run well at first, over time as the data grows, some random symptoms may show up, including database deadlocks and even some missing sales orders.

Apr 24, 2014
2 minutes

Optimize Magento Time To First Byte

When looking at the performance of your site, waterfall charts are one of the first places you should investigate. The first thing that is represented on these charts is that the HTML for the website is the first resource that is downloaded every time.

As a result of being the first resource downloaded every time, this is the logical first place to look to improve the performance of your Magento website. There are a few ways to make sure that the page downloads more quickly, and they all involve making sure that the file size is as small as possible.