Search code examples
jekyllgithub-pages

Get collection page variable by path/name as fast as possible


Say I have a Jekyll data called CardMaster (array of objects) and Jekyll collection called charas.

I have page that needs to loop for each cardInfo of CardMaster. For each cardInfo, there is one unique page in charas collection that have more info I need here.

This is my current code that works, but it is very slow (O(N^2), N is about a few hundred).

{% assign cardInfos = site.data.CardMaster | where: "stockOrder", 1 %}
<ul>
<!-- this will loop N times -->
{% for c in cardInfos %}
<li>
  <!-- print some info from `c` -->

  {% capture page_cond %}item.name == "{{ c.resourceName }}.md"{% endcapture %}
  <!-- this will take O(N) time -->
  {% assign page = site.charas | where_exp: "item", page_cond | first %}

  <!-- print some info from `page` -->
{% endfor %}
</ul>

What I wish to do is make the {% assign page = site.charas | where_exp: "item", page_cond | first %} faster, like if it is possible to do {% assign page = site.charas_page_map["{{c.resourceName}}.md"] %} to reduce the time complexity from O(N) to O(constant).

Merging Jekyll data CardMaster to Jekyll collection charas or vice versa is not feasible because CardMaster is machine generated regularly from external source while charas are maintained by human.

The website is hosted on Github Pages, so most Jekyll plugins are forbidden.


Solution

  • In the end I just implement this with custom Jekyll plugins, because it is possible to build your site in Github Actions (with whatever version of Jekyll you want) and publish to Github Pages (and the build time is faster too so that is a plus)