Files
Claude 5a370f8c78 M9: benchmarks, chunked sorted map and JSON clone for the in-memory driver
Profiling showed document updates dominated by O(n) sorted-array splices; the
chunked map makes inserts O(log n + chunk). Baselines recorded in DESIGN.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Qxbkp4tA7G5SCWjzWuxmE
2026-10-23 07:25:04 +00:00

35 lines
1.3 KiB
TypeScript

import { bench, describe } from 'vitest'
import { InMemoryKVDriver } from '../src/kv/memory.js'
import { KVStorage } from '../src/kv/node.js'
import { KVEventStorage } from '../src/events/kv.js'
import { EventSourcedDocumentStorage } from '../src/documents/event-sourced.js'
const N = 10_000
const docs = new EventSourcedDocumentStorage(new KVEventStorage(new KVStorage(new InMemoryKVDriver())), {
indexes: { users: [{ fields: ['age'] }, { fields: ['email'], unique: true }] },
warnOnFullScan: false,
})
const users = docs.child('users')
for (let i = 0; i < N; i++) await users.set(`u${i}`, { name: `user ${i}`, email: `u${i}@x.io`, age: i % 100, city: `c${i % 50}` })
let i = 0
describe(`documents @ ${N / 1000}k docs`, () => {
bench('get', async () => {
await users.get(`u${i++ % N}`)
})
bench('update ($inc, indexed node)', async () => {
await users.update(`u${i++ % N}`, { $inc: { logins: 1 } })
})
bench('findOne by unique index (email)', async () => {
await users.findOne({ email: `u${i++ % N}@x.io` })
})
bench('find indexed (age = k, ~100 hits)', async () => {
for await (const _ of users.find({ age: i++ % 100 })) {
}
})
bench('find full scan (city = k, ~200 hits)', async () => {
for await (const _ of users.find({ city: `c${i++ % 50}` })) {
}
})
})