tursodatabase / libsql-client-ts

TypeScript/JavaScript client API for libSQL
https://docs.turso.tech/sdk/ts/quickstart
MIT License
289 stars 37 forks source link

Optimize local database access #100

Closed penberg closed 1 year ago

penberg commented 1 year ago

We currently open a new SQLite connection for every SQL statemenet we execute. This is wasteful with better-sqlite3, but even more wasteful with libsql, which sets up remote connectivity in case it needs to do write delegation.

To fix the issue, let's reuse the connection in a client, which reduces local query latency by up to almost 10x:

Before:

penberg@vonneumann remote-based-sync % bun run index.ts
The keycard for user Glauber is valid. Query took 3ms
The keycard for user Glauber is valid. Query took 2ms
The keycard for user Glauber is valid. Query took 965us
The keycard for user Glauber is valid. Query took 815us
The keycard for user Glauber is valid. Query took 726us
The keycard for user Glauber is valid. Query took 740us
The keycard for user Glauber is valid. Query took 720us
The keycard for user Glauber is valid. Query took 690us
The keycard for user Glauber is valid. Query took 682us
The keycard for user Glauber is valid. Query took 701us

After:

penberg@vonneumann remote-based-sync % bun run index.ts

The keycard for user Glauber is valid. Query took 1ms
The keycard for user Glauber is valid. Query took 644us
The keycard for user Glauber is valid. Query took 296us
The keycard for user Glauber is valid. Query took 197us
The keycard for user Glauber is valid. Query took 177us
The keycard for user Glauber is valid. Query took 175us
The keycard for user Glauber is valid. Query took 192us
The keycard for user Glauber is valid. Query took 150us
The keycard for user Glauber is valid. Query took 135us
The keycard for user Glauber is valid. Query took 148us