Skip to content

Commit e0f91cc

Browse files
committed
avoid calling prop.toString so much
1 parent 2c95e3e commit e0f91cc

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

packages/toolkit/src/combineSlices.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ const stateProxyMap = new WeakMap<object, object>()
322322

323323
const createStateProxy = <State extends object>(
324324
state: State,
325-
reducerMap: Partial<Record<string, Reducer>>,
326-
initialStateCache: Record<string, unknown>,
325+
reducerMap: Partial<Record<PropertyKey, Reducer>>,
326+
initialStateCache: Record<PropertyKey, unknown>,
327327
) =>
328328
getOrInsertComputed(
329329
stateProxyMap,
@@ -333,24 +333,23 @@ const createStateProxy = <State extends object>(
333333
get: (target, prop, receiver) => {
334334
if (prop === ORIGINAL_STATE) return target
335335
const result = Reflect.get(target, prop, receiver)
336-
const propString = prop.toString()
337336
if (typeof result === 'undefined') {
338-
const cached = initialStateCache[propString]
337+
const cached = initialStateCache[prop]
339338
if (typeof cached !== 'undefined') return cached
340-
const reducer = reducerMap[propString]
339+
const reducer = reducerMap[prop]
341340
if (reducer) {
342341
// ensure action type is random, to prevent reducer treating it differently
343342
const reducerResult = reducer(undefined, { type: nanoid() })
344343
if (typeof reducerResult === 'undefined') {
345344
throw new Error(
346-
`The slice reducer for key "${propString}" returned undefined when called for selector(). ` +
345+
`The slice reducer for key "${prop.toString()}" returned undefined when called for selector(). ` +
347346
`If the state passed to the reducer is undefined, you must ` +
348347
`explicitly return the initial state. The initial state may ` +
349348
`not be undefined. If you don't want to set a value for this reducer, ` +
350349
`you can use null instead of undefined.`,
351350
)
352351
}
353-
initialStateCache[propString] = reducerResult
352+
initialStateCache[prop] = reducerResult
354353
return reducerResult
355354
}
356355
}
@@ -366,7 +365,8 @@ const original = (state: any) => {
366365
return state[ORIGINAL_STATE]
367366
}
368367

369-
const noopReducer: Reducer<Record<string, any>> = (state = {}) => state
368+
const emptyObject = {}
369+
const noopReducer: Reducer<Record<string, any>> = (state = emptyObject) => state
370370

371371
export function combineSlices<Slices extends Array<AnySliceLike | ReducerMap>>(
372372
...slices: Slices
@@ -387,7 +387,7 @@ export function combineSlices<Slices extends Array<AnySliceLike | ReducerMap>>(
387387

388388
combinedReducer.withLazyLoadedSlices = () => combinedReducer
389389

390-
const initialStateCache: Record<string, unknown> = {}
390+
const initialStateCache: Record<PropertyKey, unknown> = {}
391391

392392
const inject = (
393393
slice: AnySliceLike,

0 commit comments

Comments
 (0)