Cache block. settings in a variable
Assign block.settings to a variable instead of accessing it repeatedly in Liquid.
There's a performance cost to accessing the block.settings object each time it appears in a template. Liquid must resolve the property chain (block, then settings, then the specific key) on every access. We're working to improve this, but caching it in a variable with assign is a reliable optimization today.
The cost multiplies inside loops. Consider a section with 20 blocks, each referencing 5 settings properties. That's 20 × 5 = 100 block.settings lookups per page render. Assigning block.settings to a variable at the top of each iteration changes the math: 20 assign operations and 100 fast variable reads. Variable reads are cheaper than the full property-chain resolution.
This optimization applies equally to section.settings when a section accesses its own settings many times.
At the top of a block loop iteration, or at the top of a section that accesses section.settings many times, assign the settings object to a descriptive variable and reference that variable throughout:
Anchor to ExamplesExamples
Anchor to Block settings accessed multiple timesBlock settings accessed multiple times
Less performant:
More performant:
Anchor to Inside a for loop iterating over section.blocksInside a for loop iterating over section. blocks
This is where the optimization has the most impact. Each iteration avoids repeated property-chain resolution:
In this example, each iteration accesses block settings 9 times. With 20 blocks, that's 180 accesses. The assign at the top of each iteration turns those into 20 assigns and 180 fast variable reads instead of 180 full property-chain lookups.
Anchor to Section settings accessed many timesSection settings accessed many times
The same pattern applies to section.settings when a section accesses its own settings repeatedly:
Anchor to TestingTesting
- Use the Theme Inspector Chrome extension to measure per-section and per-block Liquid rendering time. Compare timings before and after applying the pattern.
- Compare TTFB in the Chrome DevTools Network panel before and after applying this pattern. Filter to the document request and check the Waiting for server response time.
- The impact is most visible on pages with many sections or blocks: homepages with slideshow sections, FAQ sections with many items, and any section that iterates over
section.blocks.
Anchor to ReferencesReferences
assigntagblock.settingssection.settingsfortagsection.blocks- Debugging common causes for slow loading in Shopify Liquid storefronts
- Avoid deeply nested Liquid loops
- Move operations outside loops
- Avoid repetitive filter calls