Skip to content

Commit f26a3ec

Browse files
committed
experiment: try replace building blocks weakmap with symbol for perf
1 parent c21f519 commit f26a3ec

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/vanilla/internals.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ type Store = {
156156
...args: Args
157157
) => Result
158158
sub: (atom: AnyAtom, listener: () => void) => () => void
159+
[BUILDING_BLOCKS_KEY]: Readonly<BuildingBlocks>
159160
}
160161

161162
type BuildingBlocks = [
@@ -1002,10 +1003,10 @@ const BUILDING_BLOCK_abortPromise: AbortPromise = (store, promise) => {
10021003
abortHandlers?.forEach((fn) => fn())
10031004
}
10041005

1005-
const buildingBlockMap = new WeakMap<Store, Readonly<BuildingBlocks>>()
1006+
const BUILDING_BLOCKS_KEY: unique symbol = Symbol('buildingBlocks')
10061007

10071008
const getInternalBuildingBlocks = (store: Store): Readonly<BuildingBlocks> => {
1008-
const buildingBlocks = buildingBlockMap.get(store)!
1009+
const buildingBlocks = store[BUILDING_BLOCKS_KEY]
10091010
if (import.meta.env?.MODE !== 'production' && !buildingBlocks) {
10101011
throw new Error(
10111012
'Store must be created by buildStore to read its building blocks',
@@ -1077,7 +1078,7 @@ function buildStore(...buildArgs: Partial<BuildingBlocks>): Store {
10771078
[0],
10781079
] satisfies BuildingBlocks
10791080
).map((fn, i) => buildArgs[i] || fn) as BuildingBlocks
1080-
buildingBlockMap.set(store, Object.freeze(buildingBlocks))
1081+
store[BUILDING_BLOCKS_KEY] = Object.freeze(buildingBlocks)
10811082
return store
10821083
}
10831084

0 commit comments

Comments
 (0)