Skip to content

Commit

Permalink
Add "Naive Indexed Join" to benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Dec 20, 2024
1 parent 1923023 commit 4c52d37
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion packages/d2ts-benchmark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const generateData = (size: number) => {
}

// Test data - generate 1000 items but split into initial and incremental sets
const totalSize = 10000
const totalSize = 50000
const initialSize = 9000
const incrementalRuns = totalSize - initialSize
const { users: allUsers, posts: allPosts } = generateData(totalSize)
Expand Down Expand Up @@ -87,6 +87,69 @@ joinSuite.add({
},
})

// Add naive indexed join benchmark
joinSuite.add({
name: 'Naive Indexed Join',
setup: () => ({
currentUsers: [...initialUsers],
currentPosts: [...initialPosts],
postsByUser: new Map<number, typeof initialPosts>(),
result: [] as { userName: string; postTitle: string }[],
}),
firstRun: (ctx) => {
// Build post index
ctx.postsByUser.clear()
for (const post of ctx.currentPosts) {
if (!ctx.postsByUser.has(post.userId)) {
ctx.postsByUser.set(post.userId, [])
}
ctx.postsByUser.get(post.userId)!.push(post)
}

// Compute join using index
ctx.result = []
for (const user of ctx.currentUsers) {
const userPosts = ctx.postsByUser.get(user.id) || []
for (const post of userPosts) {
ctx.result.push({
userName: user.name,
postTitle: post.title,
})
}
}
},
incrementalRun: (ctx, i) => {
const user = incrementalUsers[i]
const post1 = incrementalPosts[i * 2]
const post2 = incrementalPosts[i * 2 + 1]

// Update data
ctx.currentUsers.push(user)
ctx.currentPosts.push(post1, post2)

// Rebuild post index from scratch
ctx.postsByUser.clear()
for (const post of ctx.currentPosts) {
if (!ctx.postsByUser.has(post.userId)) {
ctx.postsByUser.set(post.userId, [])
}
ctx.postsByUser.get(post.userId)!.push(post)
}

// Recompute entire join using index
ctx.result = []
for (const user of ctx.currentUsers) {
const userPosts = ctx.postsByUser.get(user.id) || []
for (const post of userPosts) {
ctx.result.push({
userName: user.name,
postTitle: post.title,
})
}
}
},
})

// Add D2TS join benchmark
joinSuite.add({
name: 'D2TS Join',
Expand Down

0 comments on commit 4c52d37

Please sign in to comment.