Skip to content

⚡ Optimize AnalyticsService order retrieval with parallel KV lookups#57

Merged
AJFrio merged 1 commit intomainfrom
perf/analytics-service-parallel-kv-12802145833129644312
Feb 23, 2026
Merged

⚡ Optimize AnalyticsService order retrieval with parallel KV lookups#57
AJFrio merged 1 commit intomainfrom
perf/analytics-service-parallel-kv-12802145833129644312

Conversation

@AJFrio
Copy link
Copy Markdown
Owner

@AJFrio AJFrio commented Feb 23, 2026

💡 What:
I refactored the getOrders method in src/services/AnalyticsService.js to parallelize the retrieval of order fulfillment statuses from Cloudflare KV. Instead of fetching them one by one inside a loop, I now identify all candidate sessions, fetch their statuses concurrently using Promise.all, and store them in a Map for efficient lookup.

🎯 Why:
The previous implementation used await inside a for loop, causing sequential blocking. For N orders, this resulted in N sequential round-trips to the KV store. With 50 orders and a 20ms latency per KV call, this would take ~1000ms. By parallelizing these calls, the total time is reduced to roughly the latency of the slowest single request (plus some overhead), theoretically O(1) instead of O(N) relative to network latency.

📊 Measured Improvement:
I created a benchmark test tests/services/AnalyticsService_getOrders.perf.test.js simulating 50 sessions with a 20ms KV delay.

  • Baseline: ~1082ms
  • Optimized: ~73ms
  • Improvement: ~15x faster

Existing tests in tests/services/AnalyticsService.perf.test.js also passed, ensuring no regressions.


PR created automatically by Jules for task 12802145833129644312 started by @AJFrio

- Replaces sequential `await` calls in the `getOrders` filtering loop with `Promise.all` to fetch all fulfillment statuses concurrently.
- Stores fetched data in a Map to avoid redundant KV lookups during the response mapping phase.
- Improves performance significantly (benchmark shows reduction from ~1000ms to ~70ms for 50 items).

Co-authored-by: AJFrio <20246916+AJFrio@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@AJFrio AJFrio merged commit d1bf332 into main Feb 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant