Skip to content

Commit cb051f4

Browse files
committed
more accurate getItem perf
1 parent 9b4ba66 commit cb051f4

File tree

5 files changed

+12
-2
lines changed

5 files changed

+12
-2
lines changed

packages/idb-cache-app/src/App.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,16 @@ const App = () => {
6969
window.location.hash = `#${params.toString()}`;
7070
}, [itemSize]);
7171

72-
const keyCounter = useRef(0);
72+
const keyCounter = useRef(
73+
Number.parseInt(localStorage.getItem("keyCounter") || "0") || 0,
74+
);
7375
const [contentKey, saveContentKey] = useState<string>(() =>
74-
deterministicHash(`initial-seed-${keyCounter.current}`),
76+
deterministicHash(`seed-${keyCounter.current}`),
7577
);
7678

7779
const encryptAndStore = useCallback(async () => {
7880
const key = deterministicHash(`seed-${keyCounter.current}`);
81+
localStorage.setItem("keyCounter", String(keyCounter.current));
7982
keyCounter.current += 1;
8083
saveContentKey(key);
8184

@@ -127,6 +130,7 @@ const App = () => {
127130
const clear = useCallback(async () => {
128131
const start = performance.now();
129132
await cache.clear();
133+
localStorage.removeItem("keyCounter");
130134
const end = performance.now();
131135
setClearTime(end - start);
132136
}, []);
@@ -161,6 +165,7 @@ const App = () => {
161165
data-testid="reset-cacheKey"
162166
onClick={() => {
163167
localStorage.removeItem("cacheKey");
168+
localStorage.removeItem("keyCounter");
164169
window.location.reload();
165170
}}
166171
>
@@ -185,6 +190,7 @@ const App = () => {
185190
data-testid="reset-cacheBuster"
186191
onClick={() => {
187192
localStorage.removeItem("cacheBuster");
193+
localStorage.removeItem("keyCounter");
188194
window.location.reload();
189195
}}
190196
>

packages/idb-cache-app/tests/test-1.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { test } from "@playwright/test";
22

33
test("basics", async ({ page }) => {
44
await page.goto("http://localhost:3000/#size=32");
5+
await page.getByTestId("reset-cacheBuster").click();
56
await page.getByRole("button", { name: "clear" }).click();
67
await page.getByRole("button", { name: "count" }).click();
78
await page.getByText("0", { exact: true }).click();

packages/idb-cache-app/tests/test-2.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { test, expect } from "@playwright/test";
22

33
test("cache key (1)", async ({ page }) => {
44
await page.goto("http://localhost:3000/#size=32");
5+
await page.getByTestId("reset-cacheBuster").click();
56
await page.getByTestId("clear-button").click();
67
await expect(
78
page.getByTestId("count-value").getByText("------")

packages/idb-cache-app/tests/test-3.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { test, expect } from "@playwright/test";
22

33
test("cache buster (1)", async ({ page }) => {
44
await page.goto("http://localhost:3000/#size=32");
5+
await page.getByTestId("reset-cacheBuster").click();
56
await page.getByTestId("clear-button").click();
67
await page.getByTestId("count-button").click();
78
await expect(page.getByTestId("count-value").getByText("0")).toBeVisible();

packages/idb-cache-app/tests/test-4.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { test, expect } from "@playwright/test";
22

33
test("20mb size item", async ({ page }) => {
44
await page.goto("http://localhost:3000/#size=20480");
5+
await page.getByTestId("reset-cacheBuster").click();
56
await page.getByTestId("clear-button").click();
67
await page.getByTestId("set-item-button").click();
78
await expect(page.getByText("6u81xr")).toBeVisible();

0 commit comments

Comments
 (0)