if-block-optimization
Simon Holthausen 18 hours ago
parent 83516e2932
commit dd221fc8d8

@ -44,7 +44,13 @@ export function IfBlock(node, context) {
// Wrap complex expressions (anything beyond a simple identifier) in $.derived() for memoization.
// TODO check expression content more thoroughly to avoid wrapping for stuff like `foo > 1` or `foo.length`
if (branch.test.type !== 'Identifier') {
if (
branch.test.type !== 'Identifier' &&
branch.test.type !== 'Literal' &&
(branch.test.type !== 'MemberExpression' ||
// foo.bar is fine but foo[bar] is not
branch.metadata.expression.dependencies.size > 1)
) {
const derived_id = b.id(context.state.scope.generate('d'));
statements.push(b.var(derived_id, b.call('$.derived', b.arrow([], expression))));
test = b.call('$.get', derived_id);

@ -10,6 +10,7 @@ import {
} from '../hydration.js';
import { block } from '../../reactivity/effects.js';
import { BranchManager } from './branches.js';
import { HYDRATION_START, HYDRATION_START_ELSE } from '../../../../constants.js';
/**
* @param {TemplateNode} node
@ -33,15 +34,16 @@ export function if_block(node, fn, elseif = false) {
if (hydrating) {
const data = read_hydration_instruction(node);
// Parse the hydration comment to determine which branch was server-rendered:
// "[" = branch 0, "[1" = branch 1, "[2" = branch 2, ..., "[!" = else (false)
/** @type {number | false} */
/**
* @type {number | false}
* "[" = branch 0, "[1" = branch 1, "[2" = branch 2, ..., "[!" = else (false)
*/
var hydrated_key;
if (data.length === 1) {
hydrated_key = 0; // "["
} else if (data[1] === '!') {
hydrated_key = false; // "[!"
if (data === HYDRATION_START) {
hydrated_key = 0;
} else if (data === HYDRATION_START_ELSE) {
hydrated_key = false;
} else {
hydrated_key = parseInt(data.substring(1)); // "[1", "[2", etc.
}

@ -26,10 +26,8 @@ export default function Async_const($$anchor) {
$.append($$anchor, p);
};
var d = $.derived(() => true);
$.if(node, ($$render) => {
if ($.get(d)) $$render(consequent, 0);
if (true) $$render(consequent, 0);
});
}

@ -42,10 +42,8 @@ export default function Async_in_derived($$anchor, $$props) {
]);
};
var d = $.derived(() => true);
$.if(node, ($$render) => {
if ($.get(d)) $$render(consequent, 0);
if (true) $$render(consequent, 0);
});
}

Loading…
Cancel
Save