Sean Nielsen
Sean Nielsen

Reputation: 53

Jinja: FastAPI: Performance with many small files

I guess this is more related to Jinja rather than FastAPI, as this should also apply for Flask and Django I guess.

Is there any performance disadvantages of having many small files, and {% include %} them, rather than having one big html file.

in this case, I have a forms page, that is well over 200 lines. Most of the inputs can somehow be grouped together, let's use cars as an example:

Car: Make, Model, engine, fuel.

Loan: Carprice, interest.

expenses: Gas, Maintenance

so rather than listing the inputs, labels, div's etc. in one big file, I could create a file called template/partials/car.html with these inputs and call:

{% include partials/car.html %}
{% include partials/loan.html %}
{% include partials/expenses.html %}

etc.

I feel like this would make it more readable, but is it the right way to do it, and will it have an impact on performance as there will be more small IO operations?

Upvotes: 1

Views: 709

Answers (1)

amanb
amanb

Reputation: 5473

Jinja is a template engine. A template is meant to be re-usable. Its purpose is to provide a skeleton framework for logically rendering data as required. One of the major advantages of using a template engine is that it can significantly reduce several lines of code because it makes the code DRY to a large extent. So, its perfectly okay to have templates for each of the html files and then call them as you mentioned. Organizing the project in this way would also make the code more readable of course.

From my experience, I have used jinja2 to render a complete WPF project in C#.NET. This project had about 5-10 files(varies depending upon client specific requirements) that were templated with several lines of code which included functions, classes and even XAML code, all of it in C#. Overall, the lines of code was way above 200 lines and using Jinja we were able to quickly render our project for any customer requirement. We didn't face any performance issues using jinja2.

The following excerpt is from the official FAQs of jinja2 regarding performance:

Generally speaking the performance of a template engine doesn’t matter much as the usual bottleneck in a web application is either the database or the application code.

You can read more about it here

Upvotes: 3

Related Questions