Skip to content

Commit f86e8f3

Browse files
committed
Add strictIteration option
1 parent 1e53717 commit f86e8f3

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/core/immerClass.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,24 @@ interface ProducersFns {
3131
produceWithPatches: IProduceWithPatches
3232
}
3333

34-
export type StrictMode = boolean | "class_only";
34+
export type StrictMode = boolean | "class_only"
3535

3636
export class Immer implements ProducersFns {
3737
autoFreeze_: boolean = true
3838
useStrictShallowCopy_: StrictMode = false
39+
useStrictIteration_: boolean = false
3940

4041
constructor(config?: {
4142
autoFreeze?: boolean
4243
useStrictShallowCopy?: StrictMode
44+
useStrictIteration?: boolean
4345
}) {
4446
if (typeof config?.autoFreeze === "boolean")
4547
this.setAutoFreeze(config!.autoFreeze)
4648
if (typeof config?.useStrictShallowCopy === "boolean")
4749
this.setUseStrictShallowCopy(config!.useStrictShallowCopy)
50+
if (typeof config?.useStrictIteration === "boolean")
51+
this.setUseStrictIteration(config!.useStrictIteration)
4852
}
4953

5054
/**
@@ -172,6 +176,20 @@ export class Immer implements ProducersFns {
172176
this.useStrictShallowCopy_ = value
173177
}
174178

179+
/**
180+
* Pass false to use faster iteration that skips non-enumerable properties
181+
* but still handles symbols for compatibility.
182+
*
183+
* By default, strict iteration is enabled (includes all own properties).
184+
*/
185+
setUseStrictIteration(value: boolean) {
186+
this.useStrictIteration_ = value
187+
}
188+
189+
shouldUseStrictIteration(obj: any): boolean {
190+
return this.useStrictIteration_
191+
}
192+
175193
applyPatches<T extends Objectish>(base: T, patches: readonly Patch[]): T {
176194
// If a patch replaces the entire state, take that replacement as base
177195
// before applying patches

src/immer.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ export const setUseStrictShallowCopy = /* @__PURE__ */ immer.setUseStrictShallow
7171
immer
7272
)
7373

74+
/**
75+
* Pass false to use ultra-fast iteration that only processes enumerable string properties.
76+
* This skips symbols and non-enumerable properties for maximum performance.
77+
*
78+
* By default, strict iteration is enabled (includes all own properties).
79+
*/
80+
export const setUseStrictIteration = /* @__PURE__ */ immer.setUseStrictIteration.bind(
81+
immer
82+
)
83+
7484
/**
7585
* Apply an array of Immer patches to the first argument.
7686
*

0 commit comments

Comments
 (0)