-
-
Notifications
You must be signed in to change notification settings - Fork 850
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(performance): Make non-strict mode faster for classes. Addresses #…
…1071 Immer 10.x solved slow iteration for plain JS objects. This update applies the same handling to class instances. In cases this makes class instance handling 3 times faster. Note that this slightly modifies the behavior of Immer with classes in obscure corner cases, in ways that match current documentation, but do not match previous behavior. If you run into issues with this release icmw. class instances, use `setUseStrictShallowCopy("class_only")` to revert to the old behavior. For more details see https://immerjs.github.io/immer/complex-objects#semantics-in-detail
- Loading branch information
Showing
5 changed files
with
120 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,75 @@ | ||
import {produce, setUseStrictShallowCopy} from "../src/immer" | ||
import { | ||
immerable, | ||
produce, | ||
setUseStrictShallowCopy, | ||
setAutoFreeze, | ||
StrictMode | ||
} from "../src/immer" | ||
|
||
describe("setUseStrictShallowCopy(true)", () => { | ||
test("keep descriptors", () => { | ||
setUseStrictShallowCopy(true) | ||
describe.each([true, false, "class_only" as const])( | ||
"setUseStrictShallowCopy(true)", | ||
(strictMode: StrictMode) => { | ||
test("keep descriptors, mode: " + strictMode, () => { | ||
setUseStrictShallowCopy(strictMode) | ||
|
||
const base: Record<string, unknown> = {} | ||
Object.defineProperty(base, "foo", { | ||
value: "foo", | ||
writable: false, | ||
configurable: false | ||
const base: Record<string, unknown> = {} | ||
Object.defineProperty(base, "foo", { | ||
value: "foo", | ||
writable: false, | ||
configurable: false | ||
}) | ||
const copy = produce(base, (draft: any) => { | ||
draft.bar = "bar" | ||
}) | ||
if (strictMode === true) { | ||
expect(Object.getOwnPropertyDescriptor(copy, "foo")).toStrictEqual( | ||
Object.getOwnPropertyDescriptor(base, "foo") | ||
) | ||
} else { | ||
expect(Object.getOwnPropertyDescriptor(copy, "foo")).toBeUndefined() | ||
} | ||
}) | ||
const copy = produce(base, (draft: any) => { | ||
draft.bar = "bar" | ||
}) | ||
expect(Object.getOwnPropertyDescriptor(copy, "foo")).toStrictEqual( | ||
Object.getOwnPropertyDescriptor(base, "foo") | ||
) | ||
}) | ||
}) | ||
|
||
describe("setUseStrictShallowCopy(false)", () => { | ||
test("ignore descriptors", () => { | ||
setUseStrictShallowCopy(false) | ||
|
||
const base: Record<string, unknown> = {} | ||
Object.defineProperty(base, "foo", { | ||
value: "foo", | ||
writable: false, | ||
configurable: false | ||
}) | ||
const copy = produce(base, (draft: any) => { | ||
draft.bar = "bar" | ||
|
||
test("keep non-enumerable class descriptors, mode: " + strictMode, () => { | ||
setUseStrictShallowCopy(strictMode) | ||
setAutoFreeze(false) | ||
|
||
class X { | ||
[immerable] = true | ||
foo = "foo" | ||
bar!: string | ||
constructor() { | ||
Object.defineProperty(this, "bar", { | ||
get() { | ||
return this.foo + "bar" | ||
}, | ||
configurable: false, | ||
enumerable: false | ||
}) | ||
} | ||
|
||
get baz() { | ||
return this.foo + "baz" | ||
} | ||
} | ||
|
||
const copy = produce(new X(), (draft: any) => { | ||
draft.foo = "FOO" | ||
}) | ||
|
||
const strict = strictMode === true || strictMode === "class_only" | ||
|
||
// descriptors on the prototype are unaffected, so this is still a getter | ||
expect(copy.baz).toBe("FOObaz") | ||
// descriptors on the instance are found, even when non-enumerable, and read during copy | ||
// so new values won't be reflected | ||
expect(copy.bar).toBe(strict ? "foobar" : undefined) | ||
|
||
copy.foo = "fluff" | ||
// not updated, the own prop became a value | ||
expect(copy.bar).toBe(strict ? "foobar" : undefined) | ||
// updated, it is still a getter | ||
expect(copy.baz).toBe("fluffbaz") | ||
}) | ||
expect(Object.getOwnPropertyDescriptor(copy, "foo")).toBeUndefined() | ||
}) | ||
}) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters