Rails erb vs gsub dynamic template performance

In my project I am using ERB as a templating engine. We have one page that used the ERB template and took 5-7 seconds to load.

On the same page, we changed it to a dynamic template, the template values ​​are stored in the database.

sample template

    <p> {{name}} </p>
    <p> {{address}} </p>
    <p> {{mobile number}} </p>

      

I used gsub to apply the actual values ​​dynamically. In this case, the same page takes 16-20 seconds to load. I used 17 gsub for dynamic template.

  • How fast is rendering rendering? (Because I am using the same content before and after dynamic template)
  • Is there a way to improve performance?

Note: Compared to the "gsub" method, the "sub" method is fast. But in my case, I need to use the global sub (gsub).

+3


source to share


2 answers


In my case, the whole page looks like a dynamic template with 20 tags. In this case, gsub is not the correct solution to replace 20 tags.

Client side: I used a client side templating engine called Handlebar.js. Using this page my page loads at normal speed.



Server side: https://github.com/Shopify/liquid

0


source


If you store templates in a database, you can use the appropriate tools to render them. If it's erb, just use ERB , don't manually change gsub variables. The reason erb is much faster than manual gsub is because the erb template is compiled into ruby ​​code (for example, "Foo <% = bar%>" becomes "Foo" + bar) and is executed.



As I understand it, you want to store your templates in a database and display them on both the server and client. You can use mustache for this, it is inactive and safe to run on the server.

0


source







All Articles