.Site vs site - any performance advantages of either?

I understand, that .Site.something is a variable that contains the site information when the layout is loaded/parsed, and site.something is a function that gets the site information of the current page. In my mind (Homer Simpson once said he is afraid to learn new stuff because he might forget how to drink beer or something like that. this.) This means, that if I want to access site variables I will do it via site.something.

Are there any performance implications if I use only site.something because Hugo needs to do an extra step or is the variable already available and is just returned this way? Or do I miss something completely magical that is available via only either or other?

The way I understand it is:

  • Functionally, they both return Site Variables, i.e. the list here: https://gohugo.io/variables/site/
  • However, site is not bound to the . context, whereas .Site is. Meaning if you pass a dict to a partial, your partial might lose the .Site context (unless you explicitly pass it on), but you will always be able to use site to do so.

Personally, I use site, just so I don’t have to think about it.

Edit to add: (slightly off-topic)

I was looking for the release notes for when site was introduced: https://gohugo.io/news/0.53-relnotes/

site is almost 2 year old now. I feel old. :laughing:

7 Likes

Do you happen to know how $.Site compares? I see it in a lot of code. I thought I saw it in the official docs, but now I can’t find it.

An unrealistic single page site that makes 4M calls:

{{ range seq 2000 }}
  {{ range seq 2000 }}
    {{ site.Title }}
  {{ end }}
{{ end }}

Average of 3 builds: 14.7s

{{ range seq 2000 }}
  {{ range seq 2000 }}
    {{ $.Site.Title }}
  {{ end }}
{{ end }}

Average of 3 builds: 11.3s

That’s a difference of 0.0000009 seconds per call. That falls into my “who cares” category. I think the convenience of the global site function outweighs the performance difference.

2 Likes

What’s the origin of $? Is site a simple alias for $.Site?

See my response to your other question, and the documentation:
https://gohugo.io/templates/introduction/#template-context

No, it is not.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.