Discover how to improve the efficiency of Jekyll collection lookups in GitHub Pages with custom plugins for `faster` performance.
---
This video is based on the question https://stackoverflow.com/q/64290943/ asked by the user 'John London' ( https://stackoverflow.com/u/6521181/ ) and on the answer https://stackoverflow.com/a/68227327/ provided by the user 'John London' ( https://stackoverflow.com/u/6521181/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Get collection page variable by path/name as fast as possible
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Streamlining Jekyll Collection Access for Enhanced Performance
When developing a website using Jekyll, especially in a GitHub Pages environment, developers often face challenges regarding performance and the efficiency of data retrieval. A common scenario involves needing to loop through data elements, each with an associated collection item. This can lead to inefficient code that drastically slows down the rendering process. In this guide, we will explore a specific problem experienced in Jekyll regarding collection retrieval and how to solve it for a faster execution time.
The Problem: Slow Data Retrieval in Jekyll
Imagine you have a dynamic array of objects named CardMaster, which contains various information that needs to be displayed on a webpage. In addition, there's a Jekyll collection called charas, where each item corresponds to a unique page that holds more in-depth information relevant to the items in CardMaster. The challenge arises in efficiently linking items in CardMaster with their corresponding pages in charas.
The provided code snippet reveals an existing solution that, while functional, has a significant performance drawback. The code loops through CardMaster, and for each element, searches for its matching page in the charas collection. This results in a time complexity of O(N^2), where N represents the number of items, causing slow performance that can be incredibly frustrating.
The Current Approach
Here's the code that illustrates the issue:
[[See Video to Reveal this Text or Code Snippet]]
Inefficiencies Highlighted
Nested Looping: The outer loop (on cardInfos) executes N times, and for each iteration, a separate search is performed on charas, causing further delays.
O(N) Searches: The search for the corresponding page utilizes where_exp, which introduces an additional time complexity of O(N), compounding the total execution time.
The Desired Outcome
To enhance performance, the goal is to change the approach from O(N^2) to O(1) for fetching the pages from the charas collection. Ideally, you would want a quick direct lookup mechanism, such as:
[[See Video to Reveal this Text or Code Snippet]]
However, due to the constraints of Jekyll being hosted on GitHub Pages and the prohibition of many plugins, this ideal situation seems out of reach.
The Solution: Custom Jekyll Plugins
Despite the limitations, the recommended solution is to implement custom Jekyll plugins. The beauty of this approach is that it allows you to do much more than standard Jekyll provides, especially if you are building the site using GitHub Actions.
Steps to Implement Custom Plugins:
Set Up GitHub Actions: Use GitHub Actions to create workflows that build your site. This allows you to manage dependencies like custom Jekyll plugins without restriction.
Develop a Custom Plugin:
Create a plugin that will preprocess your charas collection and create a lookup hash map.
Store the mapping as charas_page_map where each key corresponds to the item's filename.
Update Your Code:
With the new mapping in place, you can access collection items in constant time.
Benefits of Custom Plugins
Performance Boost: With direct access to the Jekyll collection, each lookup becomes faster, leading to a significantly reduced page load time.
Flexibility: Custom plugins provide the flexibility to enhance your site with unique features and optimizations.
Control Over Build Process: By utilizing GitHub Actions, you can better manage your build process, ensuring up-to-date dependencies with every change.
Conclusion
In conclusion, while working with a Jekyll-based site on GitHub Pages comes with its unique set of challenges, optimizing your data access patterns through the use of custom plugins is
Информация по комментариям в разработке