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.

comments powered by Disqus

Related Posts

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.

Read More

Varnish and Magento Performance Mystery

One of the things that you think you will solve when you implement a Varnish caching server in front of a Magento website is performance problems. However, this is not necessarily the case. When I set Varnish up to cache content in front of my Magento website, I get what seems to be much improved performance. If I run a few tests in the developer tools of Chrome, it seems that the waterfall chart makes sense and the data is loaded appropriately and in a timely manner with the main HTML being downloaded in about 300ms.

Read More

Avoid jQuery.bind()

When chasing down performance issues, you never know what kind of problems you will find. I was looking for something that would cause jitter when scrolling on the page. After looking at the custom code that runs on every scroll event, I still had not found a reason for the jitter. Looking at the JavaScript CPU profile when scrolling in Chrome showed that there was an overwhelming majority of the time spent in a function in the Prototype JS library.

Read More