1

I'm using $.get to preload Mustache templates, which are stored as html files on the server:

$.get('/templates/template1.html');
$.get('/templates/template2.html');

When I need to render HTML using a template, I use $.get again to retrieve the HTML and render it.

Each time I request the cached template, a 200 response is of course received and the browser retrieves the file from cache.

I'm just wondering if there's any negative performance impact of retrieving from the browser cache - should I be storing the template content in a global Javascript variable instead?

Fijjit
  • 1,399
  • 2
  • 17
  • 31
  • Does it feel clunky? If not don't worry about it – megawac Nov 14 '13 at 23:38
  • 2
    There's *always* a negative performance impact of having to make an HTTP request, even one with no appreciable response size. If you can figure out a way (and many have) of pulling over all your templates in one chunk, that'd probably be better (unless there are thousands and thousands of them). – Pointy Nov 14 '13 at 23:38
  • I assumed there would be no HTTP request when the response is 200 - I thought it meant that the browser has identified it in its cache and returned it directly, without checking the server for a newer version? – Fijjit Nov 15 '13 at 12:06

1 Answers1

0

Not have any negative impact. It is a good technique to keep the html into a javascript variable. The ideal time is in $ (document). Ready and call "$. Get" asyncronico mode. example:

var html1 = "";
$(document).ready(function(){
  $.get("/templates/template1.html", function (html) {
  html1 = html;
  });
});
toto
  • 1,180
  • 2
  • 13
  • 30