Skip to content

⚡ perf(stripe): optimize variant update array traversals#61

Open
AJFrio wants to merge 1 commit intomainfrom
perf/optimize-variant-updates-936471685218604161
Open

⚡ perf(stripe): optimize variant update array traversals#61
AJFrio wants to merge 1 commit intomainfrom
perf/optimize-variant-updates-936471685218604161

Conversation

@AJFrio
Copy link
Copy Markdown
Owner

@AJFrio AJFrio commented Mar 6, 2026

💡 What:
Replaced O(N^2) Array.prototype.find() lookups with O(1) Map lookups inside updateProductVariants in src/services/ProductStripeService.js for both primary and secondary variant groups.

🎯 Why:
When updating a product, the service iterated through all incoming variants and used .find() on the existing variants array to locate the matching prior variant by ID. For a product with N existing variants and M updated variants, this resulted in an O(N * M) time complexity. Creating a Map of existing variants by ID before the loop reduces the lookup complexity to O(1), improving the overall time complexity to O(N + M).

📊 Measured Improvement:
Created a new performance benchmark (tests/services/ProductStripeService.perf.test.js) testing the update process with 10,000 existing primary and secondary variants.

  • Baseline: ~2700ms
  • Optimized: ~418ms
  • Improvement: ~84.5% reduction in execution time for the update operation.

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

Replaces O(N^2) Array.prototype.find() lookups with O(1) Map lookups
when matching incoming primary and secondary product variants against
existing ones during the update process in ProductStripeService.

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.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f7431ed5ef

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if (Array.isArray(updates.variants)) {
const incomingVariants = updates.variants
const existingVariants = Array.isArray(existingProduct.variants) ? existingProduct.variants : []
const existingVariantsMap = new Map(existingVariants.filter(ev => ev.id).map(ev => [ev.id, ev]))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Guard variant map build against null entries

existingVariantsMap is now built eagerly with existingVariants.filter(ev => ev.id), which throws if stored variant arrays contain null/undefined elements; this can happen because the admin update route accepts raw JSON and does not validate variant item shape before persistence (src/routes/admin/products.js). Before this commit, the find path only ran when an incoming variant had an id, so updates containing only new variants could still succeed despite malformed historical entries. With the eager map build, those same updates now fail immediately with a 500, so the map construction should be null-safe (and mirrored for variants2).

Useful? React with 👍 / 👎.

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