Liquid performance optimization patterns
A guide to micro-optimizations that compound for measurable TTFB improvements on complex themes.
Anchor to OverviewOverview
Individual Liquid micro-optimizations might appear small, but on complex themes they compound significantly. On a theme with 50 sections and multiple blocks per section, applying all patterns together can save 25 to 50 ms of TTFB. Individual patterns save 1 to 5 ms each, but the combined effect becomes noticeable.
Prioritize higher-impact optimizations first. These patterns are most effective on complex themes with many sections, blocks, or nested logic. Use the Theme Inspector Chrome extension to measure the impact on your Liquid rendering time, and compare TTFB in WebPageTest before and after applying these patterns.
Anchor to Loop optimizationsLoop optimizations
- Move operations outside loops: Move operations that produce the same result on every iteration outside the loop.
- Avoid excessive conditionals in loops: Minimize the number of conditional checks inside loops, especially for complex conditions.
Anchor to Caching patternsCaching patterns
- Assign
block.settingsto a variable: Assign theblock.settingsobject to a variable and reference the variable instead of accessing it repeatedly. - Avoid repetitive filter calls: If you're calling the same filter repeatedly with the same arguments, cache the result.
Anchor to Data access patternsData access patterns
- Filter in Liquid instead of making multiple database calls: When multiple sections or snippets use the same data but filter it differently, don't restructure the code to make separate database queries for each filtered view. Liquid filtering runs in Rust and is very fast. Making multiple database calls to prefilter data is slower because each call adds network and query overhead. Fetch the data once and filter it in Liquid.
Anchor to Code structure patternsCode structure patterns
- Use
echofor string output: Using theechotag or{{ }}output tags is faster than using string manipulation filters. - Avoid nested renders: Minimize nesting of snippets, especially inside loops.
Anchor to ReferencesReferences
Was this page helpful?