Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a note to avoid compiling the same template multiple times #172

Open
ChristopherDay opened this issue Feb 22, 2017 · 0 comments
Open

Comments

@ChristopherDay
Copy link

ChristopherDay commented Feb 22, 2017

I've been using Handlebars in my app for a few years now, after adding a new feature that would update the UI every few seconds i noticed a small memory leak with this function:

    // OLD CODE
    parseTemplate: function (templateName, options) {
    	var source   = $("#"+templateName).html();	
    	var template = Handlebars.compile(source);
    	var html    = template(options);
    	return html;
    }

    // FIXED CODE
    compiledTemplates: {},
    parseTemplate: function (templateName, options) {
    	if (!this.compiledTemplates[templateName]) {
	    	var source   = $("#"+templateName).html();
	    	var template = Handlebars.compile(source);
	    	this.compiledTemplates[templateName] = template;
    	}
    	var html    = this.compiledTemplates[templateName](options);
    	return html;
    }

Now after a day or so i found handlebars-lang/handlebars.js#271 and the correct way to do this, this is not explained anywhere (that i can see) in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant