Skip to content

Commit bfb9a4f

Browse files
committed
skip intermediate derived for simple cases
1 parent 71610e9 commit bfb9a4f

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,28 @@ export function VariableDeclaration(node, context) {
172172
);
173173
} else {
174174
const bindings = extract_paths(declarator.id);
175-
const object_id = context.state.scope.generate('$$d');
176175

177-
declarations.push(
178-
b.declarator(
179-
b.id(object_id),
180-
b.call('$.derived', rune === '$derived.by' ? value : b.thunk(value))
181-
)
182-
);
176+
const init = /** @type {CallExpression} */ (declarator.init);
177+
178+
/** @type {Identifier} */
179+
let id;
180+
let rhs = value;
181+
182+
if (init.arguments[0].type === 'Identifier') {
183+
id = init.arguments[0];
184+
} else {
185+
id = b.id(context.state.scope.generate('$$d'));
186+
rhs = b.call('$.get', id);
187+
188+
declarations.push(
189+
b.declarator(id, b.call('$.derived', rune === '$derived.by' ? value : b.thunk(value)))
190+
);
191+
}
192+
183193
for (let i = 0; i < bindings.length; i++) {
184194
const binding = bindings[i];
185195
declarations.push(
186-
b.declarator(
187-
binding.node,
188-
b.call('$.derived', b.thunk(binding.expression(b.call('$.get', b.id(object_id)))))
189-
)
196+
b.declarator(binding.node, b.call('$.derived', b.thunk(binding.expression(rhs))))
190197
);
191198
}
192199
}

0 commit comments

Comments
 (0)