From 1626da89b4dc6799c066a7b26656e0bc3c47b132 Mon Sep 17 00:00:00 2001 From: unional Date: Thu, 2 May 2024 22:56:14 -0700 Subject: [PATCH] fix: use null object as store fixes optimization #211 --- packages/stable-store/ts/store.create_store.spec.ts | 4 +++- packages/stable-store/ts/store.ctx.ts | 3 ++- packages/stable-store/ts/store.get_store.spec.ts | 4 +++- packages/stable-store/ts/store.ts | 6 +++--- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/stable-store/ts/store.create_store.spec.ts b/packages/stable-store/ts/store.create_store.spec.ts index cd3a012..bfc31ff 100644 --- a/packages/stable-store/ts/store.create_store.spec.ts +++ b/packages/stable-store/ts/store.create_store.spec.ts @@ -6,7 +6,9 @@ import { storeMap } from './store.ctx.js' afterEach(() => { idAssertions.splice(0, idAssertions.length) - storeMap.clear() + Object.keys(storeMap).forEach((k) => { + delete storeMap[k] + }) }) it('can create store with string key', () => { diff --git a/packages/stable-store/ts/store.ctx.ts b/packages/stable-store/ts/store.ctx.ts index 5703604..c0c0804 100644 --- a/packages/stable-store/ts/store.ctx.ts +++ b/packages/stable-store/ts/store.ctx.ts @@ -1,4 +1,5 @@ import type { StoreKey } from './store.internal.types.js' import type { Store } from './store.types.js' -export const storeMap = new Map, assertion?: ((id: string) => void) | undefined]>() +export const storeMap: Record, assertion?: ((id: string) => void) | undefined]> = + Object.create(null) diff --git a/packages/stable-store/ts/store.get_store.spec.ts b/packages/stable-store/ts/store.get_store.spec.ts index 79a2caa..5ed9d03 100644 --- a/packages/stable-store/ts/store.get_store.spec.ts +++ b/packages/stable-store/ts/store.get_store.spec.ts @@ -16,7 +16,9 @@ it('returns the same store if the key is the same string', () => { afterEach(() => { idAssertions.splice(0, idAssertions.length) - storeMap.clear() + Object.keys(storeMap).forEach((k) => { + delete storeMap[k] + }) }) it('can assert against string key', () => { diff --git a/packages/stable-store/ts/store.ts b/packages/stable-store/ts/store.ts index aedb138..ba251a3 100644 --- a/packages/stable-store/ts/store.ts +++ b/packages/stable-store/ts/store.ts @@ -67,7 +67,7 @@ export function createStore( options?: StoreOptions | undefined ): Store { assertID(id) - var c = storeMap.get(id) + var c = storeMap[id] if (c) { // biome-ignore lint/correctness/noInnerDeclarations: on purpose var [s, a] = c @@ -108,7 +108,7 @@ export function createStore( var onSet = listenerAdder(setListeners) var store = { get, onGet, set, onSet } - storeMap.set(id, [store, options?.idAssertion]) + storeMap[id] = [store, options?.idAssertion] return store } @@ -146,7 +146,7 @@ function listenerAdder(listeners: Array<(value: V) => void>) { */ export function getStore(id: string | symbol): Store { assertID(id) - var c = storeMap.get(id) + var c = storeMap[id] if (!c) throw new Error(`Store ${id.toString()} not found`) var [s, a] = c if (a) assertIDInternal(id, a)