bun:sqlite: In-Process C SQLite Bindings vs node-sqlite3 IPC Latency
- Last updated
- Prerequisites:
- SQL and SQLite basics
- Node.js native addons (N-API) vs built-in runtime bindings
- bun
- sqlite
- database
- ffi
- c-bindings
- performance
Read at your depth
The practical view
`bun:sqlite` is Bun's built-in, zero-dependency SQLite3 driver: `import { Database } from 'bun:sqlite'`. It provides synchronous, ultra-fast query execution: `const db = new Database('mydb.sqlite')`, `const query = db.query('SELECT * FROM users WHERE id = ?')`, `query.get(1)`. It is 3-6x faster than `better-sqlite3` and `node-sqlite3`.
The same idea in other frameworks
`import Database from 'better-sqlite3'; const db = new Database('db.sqlite');``import { DB } from 'https://deno.land/x/sqlite/mod.ts';``import { Database } from 'bun:sqlite'; const db = new Database('db.sqlite');`SQL.js (WASM SQLite compiled to WebAssembly).Legacy vs modern
Node.js better-sqlite3 vs Bun Native bun:sqlite
External npm SQLite packages require compilation tools (`node-gyp`); `bun:sqlite` is built-in and significantly faster.
const Database = require('better-sqlite3'); // ❌ Requires native compile step (node-gyp)import { Database } from 'bun:sqlite'; // ✅ NEW: Built-in, zero external dependenciesInterview gotchas
Context
Interviewers test your understanding of in-memory / local disk database performance.
The mechanical answer
`bun:sqlite` is **synchronous**. Because SQLite is an in-process, disk/memory database, running queries synchronously in C avoids the overhead of Promise allocations, microtask queue scheduling, and thread context switching. For local SQLite, synchronous execution is significantly faster than asynchronous overhead.
Trap
Do not `await` queries in `bun:sqlite` (`query.get()`, `query.all()` are synchronous methods).
Context
Checks knowledge of transaction boundaries in Bun's database driver.
The mechanical answer
Use `db.transaction()`: `const insertMany = db.transaction((users) => { for (const u of users) insertQuery.run(u); }); insertMany(userList);`. `db.transaction()` returns a wrapped function that automatically executes `BEGIN` and `COMMIT`, and automatically executes `ROLLBACK` if an exception is thrown inside the callback.
Trap
Manual `db.run('BEGIN')` strings are error-prone; always use the `db.transaction()` wrapper for automatic rollback safety.