Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

perf: compute expressions without serializing values (#2991)

Bogdan Chadkin045d39f92a16
diff --git a/apps/builder/app/shared/nano-states/props.ts b/apps/builder/app/shared/nano-states/props.ts
index 17149b6afd..3e5ce0802d 100644
--- a/apps/builder/app/shared/nano-states/props.ts
+++ b/apps/builder/app/shared/nano-states/props.ts
@@ -142,18 +142,27 @@ export const computeExpression = (
expression: string,
variables: Map<DataSource["id"], unknown>
) => {
- let code = "";
- for (const [id, value] of variables) {
- const identifier = encodeDataSourceVariable(id);
- code += `const ${identifier} = ${JSON.stringify(value)};\n`;
- }
try {
+ const usedVariables = new Map();
const transpiled = validateExpression(expression, {
effectful: true,
optional: true,
+ transformIdentifier: (identifier) => {
+ const id = decodeDataSourceVariable(identifier);
+ if (id) {
+ usedVariables.set(identifier, id);
+ }
+ return identifier;
+ },
});
+ let code = "";
+ // add only used variables in expression and get values
+ // from variables map without additional serializing of these values
+ for (const [identifier, id] of usedVariables) {
+ code += `let ${identifier} = _variables.get("${id}");\n`;
+ }
code += `return (${transpiled})`;
- const result = new Function(code)();
+ const result = new Function("_variables", code)(variables);
return result;
} catch {
// empty block