Skip to content

Commit 9b29e44

Browse files
committed
perf: toImmutable returns identity if already frozen
Skip cloning if input is already frozen - assumes if top-level is frozen, entire DS is frozen (reasonable contract in practice).
1 parent 6de7644 commit 9b29e44

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

src/domains/group/__.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ export const cloneToMut = <$Group extends Any>(group: $Group): cloneToMut<$Group
175175
* ```
176176
*/
177177
export const toImmutable = <$Group extends AnyMut>(group: $Group): toImmutable<$Group> => {
178+
if (Object.isFrozen(group)) return group as any
178179
const result: AnyMut = {}
179180
for (const k in group) {
180181
result[k] = Obj.toImmutableMut([...(group[k] as any[])]) as any

src/domains/obj/mutability.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface ErrorMutableInputImmutableOutput extends
4949
* ```
5050
*/
5151
export const toImmutable = <$obj extends object>(obj: $obj): toImmutable<$obj> => {
52-
// todo: copy trait instead of hardcoded copy logic here
52+
if (Object.isFrozen(obj)) return obj as toImmutable<$obj>
5353
const copy = (Array.isArray(obj) ? [...obj] : { ...obj }) as $obj
5454
return toImmutableMut(copy)
5555
}

0 commit comments

Comments
 (0)