Benchmarks
ant next to GORM 1.31, Bun 1.3, ent 0.14 and plain database/sql, doing the same work on one PostgreSQL 17. Plain SQL stands in for sqlc, whose generated code is a query string and a Scan. TestSameQuery in the same module checks that all four libraries return the same rows for the query on Why ant.
Setup
tools/compare runs the same operations in each library against PostgreSQL 17, on one machine: a Ryzen 5 3600, with the database in Docker on WSL2.
- Each number is the median of 10 runs. A run repeats the operation for a second, hundreds to thousands of times, and averages it.
- Every number includes the round trip to the database, which is most of the time on small queries.
- Commits don't wait for the disk (
synchronous_commit=off), so disk speed doesn't drown out the libraries on writes. - Plain SQL is the baseline: a hand-written query and
Scan, which is what sqlc generates. - ant runs with change tracking (its default) and with
NoTracking.
Green is the best library in each row.
Time
| µs per operationlower is better | ant | antNoTracking | GORM | Bun | ent | Plain SQLbaseline |
|---|---|---|---|---|---|---|
| Read 1000 rows | 2,075 | 1,692 | 3,361 | 2,370 | 2,240 | 1,692 |
| Find one by key | 442 | 397 | 405 | 384 | 398 | 398 |
| Filter, sort, 10 rows | 557 | 539 | 568 | 535 | 556 | 535 |
| 100 users, 500 posts | 2,238 | 1,774 | 2,731 | 2,030 | 2,031 | 1,581 |
| Insert one | 410 | – | 420 | 1,112 | 404 | 392 |
| Update one column | 399 | – | 407 | 424 | 402 | 390 |
| Load, edit, save | 805 | – | 818 | 812 | 1,869 | 798 |
Memory
| KiB per operationlower is better | ant | antNoTracking | GORM | Bun | ent | Plain SQLbaseline |
|---|---|---|---|---|---|---|
| Read 1000 rows | 582 | 275 | 392 | 295 | 589 | 257 |
| Find one by key | 3.8 | 3.5 | 5.7 | 6.3 | 4.9 | 1.5 |
| Filter, sort, 10 rows | 11.5 | 7.9 | 9.7 | 9.2 | 11.9 | 3.8 |
| 100 users, 500 posts | 486 | 241 | 362 | 277 | 335 | 257 |
| Insert one | 2.5 | – | 7.0 | 6.8 | 3.7 | 1.3 |
| Update one column | 2.5 | – | 5.6 | 5.0 | 3.1 | 0.3 |
| Load, edit, save | 5.8 | – | 11.2 | 11.2 | 10.7 | 1.8 |
Allocations
| Allocations per operationlower is better | ant | antNoTracking | GORM | Bun | ent | Plain SQLbaseline |
|---|---|---|---|---|---|---|
| Read 1000 rows | 6,850 | 6,794 | 14,810 | 8,790 | 15,843 | 7,772 |
| Find one by key | 67 | 52 | 89 | 41 | 126 | 36 |
| Filter, sort, 10 rows | 146 | 115 | 219 | 124 | 305 | 99 |
| 100 users, 500 posts | 5,011 | 3,144 | 9,558 | 6,218 | 8,152 | 4,063 |
| Insert one | 47 | – | 79 | 54 | 86 | 23 |
| Update one column | 27 | – | 61 | 14 | 64 | 8 |
| Load, edit, save | 116 | – | 146 | 52 | 269 | 44 |
What the numbers say
- Small queries cost about the same in every library.
- ant keeps a copy of each loaded row so
SaveChangescan find changes. That makes 1000 rows 1.2 times slower and twice the memory. WithNoTracking, ant reads them as fast as plain SQL. - ant uses the least memory of the four libraries in every row. Bun allocates the least on single-row reads and updates.
- Bun puts insert values into the SQL text, so the driver prepares every insert as a new statement.
default_query_exec_mode=simple_protocolin the DSN brings it to 452 µs. - ent's update reads the row back in a transaction, so load, edit and save takes four statements instead of two.
Building queries
tests/benchmarks measures what ant adds with no database involved:
| Operation | Time | Allocations |
|---|---|---|
| Resolving a field pointer | 15 ns | 0 |
| Building and rendering a query with two conditions, an order, and a limit | 3.6 µs | 22 |
Budgets in tests/benchmarks/allocs_test.go and tests/benchmarks/budget_test.go fail a test if these grow, or if reading, tracking, saving or inserting rows starts to allocate more for each row.
ant keeps the SQL of the statements it runs most, by their shape: Find, a read with a few plain conditions, and the insert of one entity. A second run with the same shape binds its values to the kept SQL and skips building and rendering. Anything else, such as a join, an Or or In, is built and rendered each time, which takes a few microseconds against a few hundred for the round trip.
Run them yourself
make db-up DATABASES=postgres # from the ant repo root
cd tools/compare
go test -run '^$' -bench . -benchmem -count 10 | tee new.txt
go run golang.org/x/perf/cmd/benchstat@latest new.txt