This is a short version to accommodate your limited attention span.
Full version here.
In this report, we will compare two different liquid tags:
capture. We created two pages, one using
capture and the other using only
assign, to understand the difference between their performances.
Generating the pages
To maximize the distinction between the two results, we executed each tag as many times as possible. To do this, we
generated a section file containing 100
render snippet calls. We chose 100 because using 1000 was causing memory
errors, as shown below:
We then created one snippet for
assign and one snippet for
capture. Each snippet executes the tag 1000 times.
At first, we tried to use this pattern…
compared to this
But we find out that when used without filters,
capture have practically the same performance.
So we adopted this pattern
We created a script that uses
curl to measure the time taken to render the page. We executed the script 1000 times for
each page, resulting in a total of 2000
curl requests. Each
curl request collected the following metrics:
pretransfer: The time, in seconds, it took from the start until the file transfer was just about to begin.
starttransfer: The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes the
pretransfertime and the time the server needed to calculate the result.
total: The total time, in seconds, that the full operation lasted.
size: The total amount of bytes that were downloaded.
We are also calculating derived metrics:
process_time: The difference between
process_time represents the actual time it takes for the server to render the page, which is the metric we are most
To avoid hitting the cache, a new page is created before each request.
Before proceeding with the actual test, we conducted a preliminary test with two pages: one with 1000 invocations
assign and one with 0 invocations. This was done to ensure that we were not hitting the cache and to validate our
method. The following graph shows the results for
It is evident that the tests are working, and the results are consistent with our expectations (we actually made other validations to conclude that, check the full report if interested).
- Difference in means for process_time: 0.13332 seconds (71.66%±0.02%)
- Difference in means for total: 0.13562 seconds (50.65%±0.02%)
- Difference in means for pretransfer: 0.00132 seconds (2.80%±0.01%)
- Difference in means for starttransfer: 0.13464 seconds (57.70%±0.02%)
- Difference in means for size: 0.24919 bytes (0.00%±0.00%)
The data is quite clear in saying that this
is slower than this
How much slower?
The resulting page is slower by 50% in
total time and 70% in
process_time. The absolute difference is about
0.13 seconds, which is not negligible for a server response.
So I should stop using assign?
- Cache. The page request will hit the cache almost always.
- The difference is tiny. In our tests, each tag was called
100x500x2 = 100,000times and the difference is about
0.14seconds. This should not be a problem in a real world template.
- Favour readability. Sometimes it is not possible to use
assignmakes the code cleaner. Every time you think it looks better to use
assigndon’t think about a
0.0000014seconds possible gain in performance.