You know that computing power is a factor for website speed, but did you know that how you code your application also plays a role? Some would argue that how you code your project is far more important than computing power, because you can create inefficient code that can’t scale or handle large amounts of traffic. You can add resources and computing power, but finding inefficient code is sometimes like finding a needle in a haystack. If you mainly code in PHP, here are a few tips to make it efficient and avoid major bottlenecks.
Use JSON and Not XML
XML is an older data set format. It’s used with some older APIs or when data must have extraneous attributes attached to records. If you have the option, use JSON. It’s much more lightweight, and several libraries automatically parse it for you.
Audit Your Code for Lazy Additions
It’s not unusual for developers to add unnecessary code as they become more frustrated with a module that just doesn’t seem to work and they don’t know why. It doesn’t make you a bad programmer, but the frustration puts you in a position to copy and paste code snippets, take an inefficient route to get it to work, or generally try anything under the sun to finally get your logic and syntax to work. It’s difficult to overcome hurdles and write efficient code as you struggle to understand why something isn’t working. Once you get it to work, it’s tempting to leave everything alone so that you don’t break it.
Always audit your code and go back and rewrite sloppy additions into more efficient modules. Just make sure you re-test your application even after small code changes.
Test APIs Before Implementation
APIs are a great way to integrate functionality into your application. Just as an example, if you want to integrate social media, you can use Twitter or Facebook’s API. These APIs are generally fast, but ultimately you base the speed of your pages on third-party interactions. If Facebook crashes (yes, it’s possible), then your own application crashes or slows to a crawl.
Always code for API errors and test the speed of the application without a third-party versus with its integration. Sometimes, an API can be the bottleneck and your code is perfectly efficient. You don’t know until you test both independently.
Remove Unnecessary Variable Manipulations or Calculations
Creating efficient code takes practice and expertise. It’s common for a new developer to use methods and calculations in several lines of code when only a few are necessary. Every line item counts. It’s much more efficient to write 5 lines of code than 100 that do the same functionality. You can have another developer look over your code to provide tips on how you can improve.
Another way to improve your efficiency is CodeWars.com. It’s an interactive website where you can code for specific business rules and get feedback from your peers.
Clean Up Database Connections
Most PHP programmers place database opened connections in a separate file, and then have another file that globally closes connections. If you leave database connections open, it keeps memory reserved, which eventually hogs RAM resources.
In addition to cleaning up database connections, limit the number of calls to the database. For instance, you might be tempted to pull a data set from your SQL database and loop through each record with another call to the database for each record. This can cause a huge load on your servers, and is completely inefficient. Before coding for a high-traffic application, it’s important to understand JOINs, UNION and SQL in general to minimize the number of times you need to make calls to the database.
Test SQL Queries for Speed Before Deployment
In many cases, it’s not the PHP code that’s killing speed. It’s the database queries or even the way the database tables are structured. You can be a whiz at creating PHP code, but writing SQL code and designing database architecture is a different skill set.
Always test your SQL queries for performance before you integrate them into your application. If they take too long, it’s added time to your application’s load times. Sometimes, it helps to get a database admin to help make your SQL code more efficient.
When you first deploy your application and have only a few visitors a day, any poor coding and database structures won’t visibly show any signs of performance issues. It’s when your site becomes popular and more people continue to use it that performance issues become a problem. By this time, it’s too late.
You can do a couple things to detect any performance issues:
1) Stress test your application for high-traffic loads.
2) Monitor your site.
A relatively low-cost way to monitor your site’s performance is Pingdom. You don’t need to install anything, and you can ensure that your site isn’t timing out due to crashes.
Stress testing your site for traffic loads is much more expensive and takes external QA software. Load Impact is one example of a stress testing tool. They have a free test where you can do a quick scan of your site and get load time values in milliseconds.
You can also use SaaS logging products. Loggly lets you analyze and store logs on their cloud servers, and these logs can be used to detect performance issues and downtime.
Monitoring your site shouldn’t replace a code audit, but in case you accidentally miss something, it’s a great way to get an early heads-up that your code isn’t holding up against higher traffic spikes.
The best way to avoid inefficient code is to write it the right way when it’s initially created. This can be through an audit from a third-party, having a team lead code review it before it goes to production, or even a penetration test from a security expert can help. Just remember that it’s better to find issues before they go into production instead of waiting for performance issues to happen later.