Skip to content

perf: Avoid redundant getTotalItems() calls in Navigation component #14

@yortch

Description

@yortch

User Story

As a shopper,
I want the cart item count badge in the navigation bar to update instantly and efficiently,
so that navigating the site feels snappy regardless of cart size.

Problem

In Navigation.tsx, getTotalItems() is called twice per render — once for the conditional check and once to display the value. Each call iterates the full cart items array via Array.reduce().

// Current — two reduce() calls per render
{getTotalItems() > 0 && (
  <span>
    {getTotalItems()}   {/* second call */}
  </span>
)}

Acceptance Criteria

  • getTotalItems() is called at most once per render in the Navigation component (store result in a variable)
  • The cart badge displays the correct total item count
  • If the total is 0, the badge remains hidden (same as current behavior)
  • No change to visible behavior for the user

Note: This issue is low complexity and a good candidate for a first contribution. It can be implemented in parallel with the other performance issues.

Files

  • frontend/src/components/Navigation.tsx

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions