Speed Test 2023: Next.js vs PHP

Speed Test 2023:  Next.js versus PHP 8 versus PHP 7

How much faster is Next.js than PHP really? Also, is PHP 8 faster than PHP 7? We conducted a controlled experiment to find out. Here are the results.

As 2023 began, Next.js had ended yet another year more popular than the last. Next.js is undeniably a strong web framework which is becoming increasingly renowned for its speed. So, when it comes to website speed, Next.js is often perceived as an 'upgrade' on older back-end frameworks like PHP.

But is this perception more rumor than reality? Is Next.js really faster than PHP? Also, has the update from PHP 7 from PHP 8 improved performance?

To answer these questions, we must turn to the holy grail of of modern problem solving... data! If we could track a website's loading speed, then we would be able to empirically determine whether Next.js is actually faster than PHP.

The obvious problem emerges, however, that if you choose a website built with Next.js and another built with PHP, the underlying technologies are not the only factors that will differ between the two. The two websites will normally have different content, different hosting hardware, different third-party plugins, and so on.

If we want to measure the impact of the technology choice on speed, it becomes imperative to test websites that are 'like-for-like'. That is, ideally, we'd need to analyze multiple versions of the same website with the same content and hosted on the same server, with the only difference between the versions being the core technology - that is, Next.js or PHP.

Luckily, for this test, we had exactly such a set of websites available for supportresort.com. Specifically, when we transitioned our website from PHP to Next.js, we had two versions of the same site: one built with PHP and one built with Next.js version 12. Furthermore, we could easily run the PHP site via PHP version 7 or PHP version 8 since our PHP code was compatible with both versions.

So, in this article, we find out whether Next.js really is faster than either PHP 7 or PHP 8 or both, and we find out too whether PHP 8 significantly out-performs PHP 7. Read on, and all will be revealed!

Key Findings

  • Next.js was consistently faster than both PHP 7 and PHP 8, with both PHP versions taking on average roughly 2.5 times longer than Next.js to load web page source code.
  • PHP 8 and PHP 7 were virtually identical in performance. On the web pages we tested, the average server-side load times of PHP 8 and PHP 7 were within 0.002 seconds of each other.

Methodology

How do you actually track the speed of a website though?

One way to track a website's speed is to use cURL, a command-line tool for transferring files across the Internet. cURL can be used to download the source code for any given web page, and to measure the time taken to download that source code.

In other words, cURL can measure how long it takes to collect a copy of the source code (that is, HTML and other internal or inline code) for a web page, prior to any downloading and rendering the other files referenced within the page, such as images, videos, stylesheets and so on.

What makes cURL so useful here is its in-built timer. cURL keeps track of the times when several crucial steps of loading the source HTML are reached, namely:

  • time_namelookup: the point at which cURL successfully finds the IP address of the domain name of the target website.
  • time_connect: the point at which cURL connects to the web server (IP address) and receives the TCP handshake.
  • time_appconnect: the point at which the SSL handshake is completed (for HTTPS).
  • time_pretransfer: the point immediately before cURL sends an HTTP GET request. This is the point at which the core back-end web technology begins to affect load times.
  • time_starttransfer: the point at which byte transfer begins. Also known as Time To First Byte (TTFB).
  • time_total: the point at which the web page's source code is fully loaded and the cURL call ends.

Below is a visual breakdown of when cURL collects its time measures.

curl request times flowchart diagrams.net
Flowchart breaking down when cURL records its loading times.

We used cURL to test the speed of three web pages on each of our three versions of supportresort.com (that is, the Next.js, PHP 7 and PHP 8 versions). Specifically, cURL requests were sent to the home page, a page about python developers, and a page discussing Kerala - the state in which the Support Resort Indian offices are situated.

To counter the natural randomness of page loading, 1,000 requests were made to each web page on each version of the website, comprising 9,000 page requests in total - three sites x three pages x 1,000 requests per page.

We compared the average speeds to determine which technology was the fastest of the three.

Obviously, there is more to a website than its source code. What this cURL approach does not take into account is image rendering, JavaScript execution and so on, meaning that the 'total' time found by cURL would most often not be reflective of the time it takes for the web page to actually fully load in the user's browser. Since we are only dealing with back-end frameworks though, comparing speeds using cURL's timing features is still appropriate for our purposes.

It is also worth noting that the cURL requests to supportresort.com were sent from a server in close geographic proximity to the target website. All times were therefore much faster than what would be typical for a remotely-situated visitor to supportresort.com.

Results - Next.js vs PHP 8 vs PHP 7 Performance

The charts below show how quickly Next.js and PHP loaded each of the three web pages in our test. There is a separate chart for each page, but the speed pattern is very similar in each case, with Next.js out-performing both PHP 7 and PHP 8.

For each of the charts, the x-axis shows the different time points used by cURL, while the y-axis shows the average time taken to reach the cURL time points. Note that the lower the line, the faster the web page speed. Note too that the PHP 7 and PHP 8 lines overlap throughout, meaning that there was no significant speed difference between the two PHP versions.

The average times to reach the pre-transfer phase were identical between the three technologies. This was entirely expected since the choice of back-end technology does not affect the pre-transfer phase. However, differences between Next.js and PHP began to emerge after the pre-transfer phase was complete.

Next.js clearly performed better than either PHP 7 or PHP 8 on all of our three web pages. In fact, for the Python developer page, after time_pretransfer, PHP took on average almost three times longer than Next.js to reach time_starttransfer, meaning that PHP took significantly longer to prepare to start transferring the page source code. This is the period in which the back-end technology was producing the source code on the server side, with Next.js being clearly more efficient in this respect.

Next.js vs PHP 8 vs PHP 7 performance  speed curl time_pretransfer time_starttransfer time_total lineplot matplotlib
Average recorded times for each technology at all stages of file transfer across the three webpages.

Next.js was a little slower than PHP when actually transferring data from the server to the visitor. While both PHP 7 and PHP 8 completed the data transfer (from first to last byte) rapidly in under 0.0001 seconds, Next.js averaged as long as 0.004 seconds.

As for the performance of different PHP versions, it seems that there is practically no difference in the load speeds of PHP 8 and PHP 7. Across all three web pages, the average completion times of the PHP 7 and PHP 8 cURL requests were never more than 0.0017 seconds apart. In a separate speed test based on a different website, we had similar findings, with PHP 8 and PHP 7 consistently yielding near identical results.

Another way to visualize the loading speeds of the different technologies is to consider the distribution (spread) of the recorded times for our 9,000 individual page requests.

By considering the distributions of the times, we can better visualize how the different technologies perform throughout a cURL request. Below is a chart which uses plots of the distributions of the times. More technically, these plots show the histograms of the data with approximated probability density functions found using kernel density estimation.

Again, Next.js is shown in light blue, PHP 7 in orange, and PHP 8 in green. In the pre-transfer phase, the time taken for all three technologies is similar so their distributions overlap. However, Next.js has clearly achieved a speed advantage by the time the bytes start getting transferred (time_starttransfer), then loses a little of that lead by the time the transfer has been completed.

Next.js vs PHP 8 vs PHP 7 performance curl time_pretransfer time_starttransfer time_total seaborn distplot
Distributions of cURL request times at file transfer stages affected by web framework.

Using a more condensed version of the plot above, we have overlaid below the distributions of times for each web page. Again, the shaded blue area sits to the left of the other shaded area, indicating that Next.js (blue) was faster than both PHP 7 (orange) and PHP 8 (green), with the speed distributions of the two versions of PHP being virtually indistinguishable from each other.

Next.js vs PHP 8 vs PHP 7 performance curl time_total time_starttransfer seaborn distplot
Distribution of times of first and last bytes transferred for each technology across the three webpages.

These distribution plots show just how consistently better Next.js was compared to PHP. The vast majority of cURL requests sent to the Next.js site were completed faster than the vast majority of requests sent to either PHP site, no matter which web page the requests were being sent to.

Note though that in absolute terms, the web page speed difference between Next.js and PHP was fairly small. That is, fast websites can be built with either Next.js or PHP, but if speed is a very high priority for you, then you may prefer to build your site with Next.js unless you have specific reasons to use PHP instead. Both remain great choices for most purposes.

Conclusion

Both Next.js and PHP are fast technologies but Next.js is the faster of the two when it comes to preparing and delivering web page source code.

Of course, all data above was collected on the single website, and results may change depending on website design. So, which framework is best for any given website may be different to the one found in this article. Certainly, though, Next.js produced the best performance in our tests.

CodingDevelopmentFaster Websites