Skip to content

Commit

Permalink
refactor: remove memoization from getFieldPath method
Browse files Browse the repository at this point in the history
Using memoization executes a function for every field and returns
a new anonymous function and this entire process is slow. So first
we should check if "getFieldPath" method is in hot path at runtime,
before using memoization
  • Loading branch information
thetutlage committed Mar 28, 2024
1 parent ea53161 commit b939931
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 146 deletions.
10 changes: 0 additions & 10 deletions src/scripts/define_inline_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ export function defineInlineFunctions(options: { convertEmptyStringsToNull: bool
field.isValid = false;
errorReporter.report(messagesProvider.getMessage(message, rule, field, args), rule, field, args);
};
function memo(fn) {
let value;
return () => {
if (value !== undefined) {
return value
}
value = fn()
return value
}
};
function defineValue(value, field) {
${options.convertEmptyStringsToNull ? `if (value === '') { value = null; }` : ''}
field.value = value;
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/field/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export function defineFieldVariables({
meta: meta,
name: ${fieldNameExpression},
wildCardPath: '${wildCardPath}',
getFieldPath: memo(() => {
getFieldPath() {
return ${fieldPathOutputExpression};
}),
},
mutate: defineValue,
report: report,
isValid: true,
Expand Down
48 changes: 24 additions & 24 deletions tests/unit/nodes/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: '',`,
` wildCardPath: '',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return '';`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand All @@ -76,9 +76,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: root_item_i,`,
` wildCardPath: '*',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return root_item_i;`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand Down Expand Up @@ -135,9 +135,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: '',`,
` wildCardPath: '',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return '';`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand All @@ -159,9 +159,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: root_item_i,`,
` wildCardPath: '*',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return root_item_i;`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand Down Expand Up @@ -214,9 +214,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: '',`,
` wildCardPath: '',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return '';`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand All @@ -235,9 +235,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: root_item_i,`,
` wildCardPath: '*',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return root_item_i;`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand Down Expand Up @@ -296,9 +296,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: '',`,
` wildCardPath: '',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return '';`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand All @@ -317,9 +317,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: root_item_i,`,
` wildCardPath: '*',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return root_item_i;`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand Down Expand Up @@ -399,9 +399,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: '',`,
` wildCardPath: '',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return '';`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand All @@ -423,9 +423,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: root_item_i,`,
` wildCardPath: '*',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return root_item_i;`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand All @@ -441,9 +441,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: 'contacts',`,
` wildCardPath: '*.contacts',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return root_item_item.getFieldPath() + '.' + 'contacts';`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand All @@ -461,9 +461,9 @@ test.group('Array node', () => {
` meta: meta,`,
` name: contacts_3_i,`,
` wildCardPath: '*.contacts.*',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return contacts_3.getFieldPath() + '.' + contacts_3_i;`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/nodes/literal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ test.group('Literal node', () => {
` meta: meta,`,
` name: '',`,
` wildCardPath: '',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return '';`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand Down Expand Up @@ -93,9 +93,9 @@ test.group('Literal node', () => {
` meta: meta,`,
` name: '',`,
` wildCardPath: '',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return '';`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand Down Expand Up @@ -145,9 +145,9 @@ test.group('Literal node', () => {
` meta: meta,`,
` name: '',`,
` wildCardPath: '',`,
` getFieldPath: memo(() => {`,
` getFieldPath() {`,
` return '';`,
` }),`,
` },`,
` mutate: defineValue,`,
` report: report,`,
` isValid: true,`,
Expand Down
Loading

0 comments on commit b939931

Please sign in to comment.