Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions benchmarks/navbar-collections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { performance } from 'perf_hooks';

// Setup mock data
const NUM_COLLECTIONS = 100;
const NUM_PRODUCTS = 5000;

const collectionsData = Array.from({ length: NUM_COLLECTIONS }, (_, i) => ({
id: `collection_${i}`,
name: `Collection ${i}`
}));

const productsData = Array.from({ length: NUM_PRODUCTS }, (_, i) => ({
id: `product_${i}`,
collectionId: `collection_${Math.floor(Math.random() * NUM_COLLECTIONS)}`,
name: `Product ${i}`
}));

function baseline() {
const start = performance.now();
const collectionsWithProducts = collectionsData.map(collection => ({
...collection,
products: productsData.filter(product => product.collectionId === collection.id)
}));
return performance.now() - start;
}

function optimized() {
const start = performance.now();
const productsByCollectionId = productsData.reduce((acc, product) => {
if (!acc[product.collectionId]) {
acc[product.collectionId] = [];
}
acc[product.collectionId].push(product);
return acc;
}, {});

const collectionsWithProducts = collectionsData.map(collection => ({
...collection,
products: productsByCollectionId[collection.id] || []
}));
return performance.now() - start;
}

const iterations = 100;
let baselineTotal = 0;
let optimizedTotal = 0;

// Warmup
for (let i=0; i<10; i++) {
baseline();
optimized();
}

for (let i = 0; i < iterations; i++) {
baselineTotal += baseline();
optimizedTotal += optimized();
}

console.log(`Baseline Avg: ${(baselineTotal / iterations).toFixed(2)} ms`);
console.log(`Optimized Avg: ${(optimizedTotal / iterations).toFixed(2)} ms`);
10 changes: 9 additions & 1 deletion src/components/storefront/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,17 @@ export function Navbar({ previewSettings, disableNavigation }) {
setCollections(collectionsData)

// Group products by collection
const productsByCollectionId = productsData.reduce((acc, product) => {
if (!acc[product.collectionId]) {
acc[product.collectionId] = []
}
acc[product.collectionId].push(product)
Comment on lines +45 to +48
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 Use a prototype-safe key map for collection grouping

The new grouping logic stores products in a plain object keyed by product.collectionId, but IDs come from API data and can collide with inherited keys like constructor or toString. In those cases if (!acc[id]) incorrectly treats the inherited function as an existing bucket and acc[id].push(...) throws, which breaks navbar rendering for affected stores; the previous filter-based version did not have this failure mode. Use Object.create(null) or a Map (or at least Object.hasOwn) for bucket lookups.

Useful? React with πŸ‘Β / πŸ‘Ž.

return acc
}, {})

const collectionsWithProducts = collectionsData.map(collection => ({
...collection,
products: productsData.filter(product => product.collectionId === collection.id)
products: productsByCollectionId[collection.id] || []
}))

setCollectionsWithProducts(collectionsWithProducts)
Expand Down