-
Notifications
You must be signed in to change notification settings - Fork 2
⚡ Optimize Navbar rendering by memoizing derived collection data #63
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import React, { useMemo, useState } from 'react'; | ||
|
|
||
| // A synthetic version of the work we want to measure | ||
| function runBenchmark() { | ||
| const collectionCount = 100; | ||
| const productCount = 1000; | ||
| const iterations = 10000; | ||
|
|
||
| const collectionsData = Array.from({ length: collectionCount }, (_, i) => ({ id: `col-${i}`, name: `Collection ${i}` })); | ||
| const productsData = Array.from({ length: productCount }, (_, i) => ({ id: `prod-${i}`, name: `Product ${i}`, collectionId: `col-${i % collectionCount}` })); | ||
|
|
||
| console.time('baseline'); | ||
| for (let i = 0; i < iterations; i++) { | ||
| const collectionsWithProducts = collectionsData.map(collection => ({ | ||
| ...collection, | ||
| products: productsData.filter(product => product.collectionId === collection.id) | ||
| })); | ||
| } | ||
| console.timeEnd('baseline'); | ||
| } | ||
|
|
||
| runBenchmark(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import React, { useMemo, useState } from 'react'; | ||
|
|
||
| // A synthetic version of the work we want to measure | ||
| function runBenchmark() { | ||
| const collectionCount = 100; | ||
| const productCount = 1000; | ||
| const iterations = 10000; | ||
|
|
||
| const collectionsData = Array.from({ length: collectionCount }, (_, i) => ({ id: `col-${i}`, name: `Collection ${i}` })); | ||
| const productsData = Array.from({ length: productCount }, (_, i) => ({ id: `prod-${i}`, name: `Product ${i}`, collectionId: `col-${i % collectionCount}` })); | ||
|
|
||
| console.time('optimized'); | ||
| for (let i = 0; i < iterations; i++) { | ||
| const productsByCollection = 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: productsByCollection[collection.id] || [] | ||
| })); | ||
| } | ||
| console.timeEnd('optimized'); | ||
| } | ||
|
|
||
| runBenchmark(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import React, { useMemo, useState } from 'react'; | ||
|
|
||
| // A synthetic version of the work we want to measure | ||
| function runBenchmark() { | ||
| const collectionCount = 100; | ||
| const productCount = 1000; | ||
| const iterations = 10000; | ||
|
|
||
| const collectionsData = Array.from({ length: collectionCount }, (_, i) => ({ id: `col-${i}`, name: `Collection ${i}` })); | ||
| const productsData = Array.from({ length: productCount }, (_, i) => ({ id: `prod-${i}`, name: `Product ${i}`, collectionId: `col-${i % collectionCount}` })); | ||
|
|
||
| console.time('optimized Map'); | ||
| for (let i = 0; i < iterations; i++) { | ||
| const productsByCollection = new Map(); | ||
| for (const product of productsData) { | ||
| const collectionProducts = productsByCollection.get(product.collectionId); | ||
| if (collectionProducts) { | ||
| collectionProducts.push(product); | ||
| } else { | ||
| productsByCollection.set(product.collectionId, [product]); | ||
| } | ||
| } | ||
|
|
||
| const collectionsWithProducts = collectionsData.map(collection => ({ | ||
| ...collection, | ||
| products: productsByCollection.get(collection.id) || [] | ||
| })); | ||
| } | ||
| console.timeEnd('optimized Map'); | ||
| } | ||
|
|
||
| runBenchmark(); |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| <link rel="icon" type="image/svg+xml" href="/cart.png" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>OpenShop</title> | ||
| <script type="module" crossorigin src="/assets/index-BYJaIYQ8.js"></script> | ||
| <script type="module" crossorigin src="/assets/index-ENYPUJjh.js"></script> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| <link rel="stylesheet" crossorigin href="/assets/index-q4lwPvEs.css"> | ||
| </head> | ||
| <body> | ||
|
|
||
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.
This new benchmark file introduces unused imports (
useMemo,useState) and an unused local result, which violates the repo’sno-unused-varsrule for**/*.{js,jsx}and causes lint failures (the same pattern appears inbenchmark2.jsandbenchmark3.js). As committed, these added scripts break standard lint runs.Useful? React with 👍 / 👎.