Skip to content

Developers · Best Practices

Measuring the Execution Time of Liquid Code

Last updated 7 November 2026

This guide shows you how to measure the execution time of Liquid code fragments using the filter. You can use this method to find out which part of a page you can modify to improve execution times.

Prerequisites

This guide is an advanced tutorial. To follow it, you must understand basic Insites concepts such as the topics in the Get Started section, the and Liquid filters and how to create application logs.

  • Development Workflow
  • Get Started
  • Liquid filter: time_diff
  • Liquid filter: to_time
  • Creating Application Logs
  • Steps

    Measuring the execution time of Liquid code fragments using the Liquid filter is a three-step process:

    • Step 1: Identifying measuring points
    • Step 2: Adding the 'to_time' and 'time_diff' filters
    • Step 3: Checking results in the logs

    Step 1: Identifying measuring points

    Locate the part of the code you want to measure. Take a look at this sample snippet:

    {% graphql g = "modules/n_plus_one/n1_programmers" %}
    
    {% for programmer in g.programmers.results %}
      <tr>
        <td>{{ programmer.properties.email }}</td>
        <td>{{ programmer.properties.title }}</td>
        <td>{{ programmer.company.properties.name }}</td>
      </tr>
    {% endfor %}

    Two items could potentially cause results to be displayed slowly:

    1. GraphQL query
    2. for loop rendering records

    Step 2: Adding the 'to_time' and 'time_diff' filters

    Use the and filters to measure execution times.
    The and variables store partial execution times in milliseconds. The variable stores the overall execution time of the code block.

    {% liquid
      assign __start = "now" | to_time
      graphql g = "modules/n_plus_one/n1_programmers"
      assign __graph = "now" | to_time
    %}
    
    {% for programmer in g.programmers.results %}
      <tr>
        <td>{{ programmer.properties.email }}</td>
        <td>{{ programmer.properties.title }}</td>
        <td>{{ programmer.company.properties.name }}</td>
      </tr>
    {% endfor %}
    
    {% liquid
      assign __render = "now" | to_time
      assign _total_duration = __start | time_diff: __render
      assign _graph_duration = __start | time_diff: __graph
      assign _render_duration = __graph | time_diff: __render
      log _total_duration, type: "benchmark-total"
      log _graph_duration, type: "benchmark-graph"
      log _render_duration, type: "benchmark-render"
    %}

    Step 3: Checking results in the logs

    Test results are sent to your instance log stream and can be accessed by using the tool.

    Next Steps

    Congratulations! You now know how to measure the execution time of Liquid code fragments using the filter. You may visit another tutorial that uses this method for testing two different solutions: Loading Related Records