Skip to content

Commit 5547315

Browse files
sledorzeclaudeeffect-bot
authored
Speed up assignProperties with an Object.assign fast path (#9511)
Co-authored-by: Claude Sonnet 5 <[email protected]> Co-authored-by: Effect Bot <[email protected]>
1 parent fed70de commit 5547315

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Speed up field copying in `Data.Class`, `Data.Error`, `Request.Class` and `Persistable` constructors.

‎packages/effect/src/internal/record.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export function assignProperty(self: object, key: PropertyKey, value: unknown):
1414

1515
/** @internal */
1616
export function assignProperties(self: object, source: object): void {
17+
// Object.assign would fire the inherited __proto__ setter for an own __proto__ key,
18+
// so use the safe per-property loop in that case.
19+
if (!Object.hasOwn(source, "__proto__")) {
20+
Object.assign(self, source)
21+
return
22+
}
1723
for (const key of Reflect.ownKeys(source)) {
1824
if (Object.prototype.propertyIsEnumerable.call(source, key)) {
1925
assignProperty(self, key, (source as any)[key])

‎packages/effect/test/Data.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ describe("Data", () => {
4242
const p = new Person({ name: "D", age: 2 })
4343
assert.deepStrictEqual(Object.keys(p).sort(), ["age", "name"])
4444
})
45+
46+
it("copies own enumerable string and symbol keys only", () => {
47+
const sym = Symbol("sym")
48+
const args: any = { name: "E", age: 3, [sym]: "value" }
49+
Object.defineProperty(args, "hidden", { value: 1, enumerable: false })
50+
const p: any = new Person(args)
51+
assert.strictEqual(p[sym], "value")
52+
assert.deepStrictEqual(Reflect.ownKeys(p), ["name", "age", sym])
53+
})
54+
55+
it("keeps the class prototype when args has an own __proto__ key", () => {
56+
const args = JSON.parse(`{"name":"F","age":4,"__proto__":{"polluted":true}}`)
57+
const p = new Person(args)
58+
assert.strictEqual(Object.getPrototypeOf(p), Person.prototype)
59+
assert.strictEqual(Object.getOwnPropertyDescriptor(p, "__proto__")?.value, args.__proto__)
60+
assert.strictEqual(p.name, "F")
61+
})
4562
})
4663

4764
describe("TaggedClass", () => {

0 commit comments

Comments
 (0)