Skip to content

Commit cb4c043

Browse files
authored
Merge pull request #127 from gadget-inc/empty-array-opt
Don't spread/allocate empty arrays when fast instantiating
2 parents ac90059 + 83639d5 commit cb4c043

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/fast-instantiator.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,21 @@ export class InstantiatorBuilder<T extends IClassModelType<Record<string, IAnyTy
333333
// Directly assignable types are primitives so we don't need to worry about setting parent/env/etc. Hence, we just
334334
// pass the snapshot straight through to the constructor.
335335
return `
336-
this["${key}"] = new QuickArray(
337-
${this.alias(`model.properties["${key}"]`)},
338-
this,
339-
context,
340-
...(snapshot?.["${key}"] ?? [])
341-
);
336+
const snapshotData = snapshot?.["${key}"];
337+
if (snapshotData && snapshotData.length > 0) {
338+
this["${key}"] = new QuickArray(
339+
${this.alias(`model.properties["${key}"]`)},
340+
this,
341+
context,
342+
...snapshotData
343+
);
344+
} else {
345+
this["${key}"] = new QuickArray(
346+
${this.alias(`model.properties["${key}"]`)},
347+
this,
348+
context
349+
);
350+
}
342351
`;
343352
}
344353

0 commit comments

Comments
 (0)