Choosing Between PHP, Lua, Go
Performance comparison of PHP, Lua (OpenResty), and Go for RESTful JSON services.
I have recently spent quite a bit of time trying to come up with a better way to implement RESTful service that serve data in JSON. This is because most of our work these days involves writing client-side JavaScript apps that present a rich UI in the browser and then do all server-side interactions via AJAX.
In pursuit of the best way to implement RESTful service on the backend, I have tried my hands at Go programming language, D programming language, Lua and of course Java and PHP. I was assuming that the Go or Lua would be much faster than PHP. Well, it turns out that at least without special optimizations, that is not the case.
I wrote programs in each language that simply query a table and return all rows and columns from that table as a JSON array.
Performance Comparison:
| Language/API | Average time/request |
|---|---|
| PHP PDO API | 0.519 ms |
| PHP Drupal API | 4.281 ms |
| LuaJIT OpenResty API with code cache ON | 0.877 ms |
| LuaJIT OpenResty API with code cache OFF | 2.191 ms |
| Go lang with GORM and Go-JSON-REST | 0.880 ms |
So it turns out that pure PHP PDO API version is the fastest. LuaJIT with code cache ON is a close second. PHP with Drupal API version is slowest because it incurs the overhead of bootstrapping Drupal. And the Go version is about same as LuaJIT.
As you can see, that PHP is still the fastest, even without APC. But in reality, the raw performance of the program itself won’t matter, because running from across the internet, the network latency will add about another 100ms. So I’m not sure why I’d switch away from PHP just yet.