Skip to content

Commit

Permalink
perf: cache existence of default props
Browse files Browse the repository at this point in the history
  • Loading branch information
ascott18 committed Nov 16, 2023
1 parent af6879d commit 444114b
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/coalesce-vue/src/viewmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1159,13 +1159,21 @@ export abstract class ViewModel<
this.$loadDirtyData(initialDirtyData);
}

for (const prop of Object.values($metadata.props)) {
if (
"defaultValue" in prop &&
(!initialDirtyData || !(prop.name in initialDirtyData))
) {
(this as any)[prop.name] = prop.defaultValue;
const ctor = this.constructor as any;
if (ctor.hasPropDefaults !== false) {
for (const prop of Object.values($metadata.props)) {
if ("defaultValue" in prop) {
ctor.hasPropDefaults ??= true;

if (!initialDirtyData || !(prop.name in initialDirtyData)) {
(this as any)[prop.name] = prop.defaultValue;
}
}
}

// Cache that this type doesn't have prop defaults so we don't
// ever have to loop over the props looking for them on future instances.
ctor.hasPropDefaults ??= false;
}
}
}
Expand Down

0 comments on commit 444114b

Please sign in to comment.