-
Notifications
You must be signed in to change notification settings - Fork 1
⚡ Optimize getProductsByCollection with secondary index #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AJFrio
merged 1 commit into
main
from
perf-optimize-kv-collection-index-17260775420947353121
Feb 9, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
|
|
||
| import { KVManager } from '../src/lib/kv.js'; | ||
| import { performance } from 'perf_hooks'; | ||
|
|
||
| // Mock KV namespace that counts operations | ||
| function createCountingMockKV() { | ||
| const store = new Map(); | ||
| let counts = { get: 0, put: 0, delete: 0, list: 0 }; | ||
|
|
||
| return { | ||
| get: async (key) => { | ||
| counts.get++; | ||
| return store.get(key) || null; | ||
| }, | ||
| put: async (key, value) => { | ||
| counts.put++; | ||
| store.set(key, value); | ||
| }, | ||
| delete: async (key) => { | ||
| counts.delete++; | ||
| store.delete(key); | ||
| }, | ||
| list: async (options) => { | ||
| counts.list++; | ||
| const keys = Array.from(store.keys()); | ||
| const filtered = options?.prefix | ||
| ? keys.filter(k => k.startsWith(options.prefix)) | ||
| : keys; | ||
| return { | ||
| keys: filtered.map(key => ({ name: key })), | ||
| list_complete: true, | ||
| cursor: '' | ||
| }; | ||
| }, | ||
| getCounts: () => ({ ...counts }), | ||
| resetCounts: () => { | ||
| counts = { get: 0, put: 0, delete: 0, list: 0 }; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| async function runBenchmark() { | ||
| const kv = createCountingMockKV(); | ||
| const kvManager = new KVManager(kv); | ||
|
|
||
| const productCount = 1000; | ||
| const collectionCount = 10; | ||
| const targetCollectionId = 'coll_target'; | ||
|
|
||
| console.log(`Setting up benchmark with ${productCount} products across ${collectionCount} collections...`); | ||
|
|
||
| // Create products | ||
| const productIds = []; | ||
| for (let i = 0; i < productCount; i++) { | ||
| const collId = i < 100 ? targetCollectionId : `coll_${i % (collectionCount - 1)}`; | ||
| const product = { | ||
| id: `prod_${i}`, | ||
| name: `Product ${i}`, | ||
| collectionId: collId | ||
| }; | ||
| await kvManager.createProduct(product); | ||
| productIds.push(product.id); | ||
| } | ||
|
|
||
| kv.resetCounts(); | ||
|
|
||
| console.log(`\nBenchmarking getProductsByCollection for '${targetCollectionId}' (contains 100 products)...`); | ||
|
|
||
| const start = performance.now(); | ||
| const products = await kvManager.getProductsByCollection(targetCollectionId); | ||
| const end = performance.now(); | ||
|
|
||
| const counts = kv.getCounts(); | ||
|
|
||
| console.log(`Results:`); | ||
| console.log(`- Found: ${products.length} products`); | ||
| console.log(`- Time: ${(end - start).toFixed(2)}ms`); | ||
| console.log(`- KV Get calls: ${counts.get}`); | ||
| console.log(`- KV Put calls: ${counts.put}`); | ||
| console.log(`- KV Delete calls: ${counts.delete}`); | ||
| console.log(`- KV List calls: ${counts.list}`); | ||
| console.log(`- Total KV calls: ${counts.get + counts.put + counts.delete + counts.list}`); | ||
| } | ||
|
|
||
| runBenchmark().catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a collection’s index key doesn’t exist (e.g., products created before this change or after a partial restore),
getProductsByCollectionnow returns[]instead of the matching products. This is a behavior regression from the previous full-scan implementation and will surface in any environment with pre-existing KV data that hasn’t been reindexed. Consider falling back to a scan or rebuilding the index whencollection:products:${collectionId}is absent to preserve correctness.Useful? React with 👍 / 👎.