Hello, I really like Swift and from time to time I go back to the server community to see how much have evolved and if I can think of using it for work or some side project. I am already using Swift for some CLI application so would be nice to use it for some web apps.
I am also in the process of learning Go, so out of curiosity I wanted to see how they compare in terms of speed. My day to day language is PHP so I added it to the test to have a baseline.
I generated a simple SQLite database with one table and ~10k rows a copied it on every project without modifications.
PHP
Brand new Laravel application with one single route, I am using Laravel Octane, with the Swoole extension. The code:
Route::get('/', function () {
return Product::all()->toJson();
});
Swift
Brand new Vapor application, Swift 5.9, Fluent and Fluent SQLite Driver, one single route where I fetch everything e return it as json (I added Content
to my Model).
import Vapor
func routes(_ app: Application) throws {
app.get { req async throws in
return try await Product.query(on: app.db).all()
}
}
Go
One main.go file, server built with Fiber and for the database I used GORM. This is the whole code:
package main
import (
"github.com/gofiber/fiber/v2"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Product struct {
ID uint `gorm:"primarykey" json:"id"`
Code string `json:"code"`
Price int `json:"price"`
}
func main() {
app := fiber.New()
db, err := gorm.Open(sqlite.Open("database.sqlite"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
app.Get("/", func(c *fiber.Ctx) error {
products := &[]Product{}
db.Find(&products)
return c.JSON(products)
})
app.Listen(":3000")
}
These are the results, in this order from left to right: Swift, Go and PHP
Swift seems quite slow, does to look right to you? Maybe I am missing something.
P.S. This was done on my Macbook Pro M2 Pro