]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Proper template string caching
authorEvert Timberg <evert.timberg@gmail.com>
Sat, 15 Aug 2015 14:56:55 +0000 (10:56 -0400)
committerEvert Timberg <evert.timberg@gmail.com>
Sat, 15 Aug 2015 14:56:55 +0000 (10:56 -0400)
src/core/core.helpers.js

index 8f52854d97b120e8e2f6d359c8aad7b75ef99d72..2fbb644a9080cab5a925579df31662e79256239e 100644 (file)
                // Blows up jshint errors based on the new Function constructor
                //Templating methods
                //Javascript micro templating by John Resig - source at http://ejohn.org/blog/javascript-micro-templating/
+               templateStringCache = {},
                template = helpers.template = function(templateString, valuesObject) {
 
                        // If templateString is function rather than string-template - call the function for valuesObject
                                return templateString(valuesObject);
                        }
 
-                       var cache = {};
-
                        function tmpl(str, data) {
                                // Figure out if we're getting a template, or if we need to
                                // load the template - and be sure to cache the result.
-                               var fn = !/\W/.test(str) ?
-                                       cache[str] = cache[str] :
+                               var fn;
 
+                               if (templateStringCache.hasOwnProperty(str)) {
+                                       fn = templateStringCache[str];
+                               } else {
                                        // Generate a reusable function that will serve as a template
                                        // generator (and which will be cached).
-                                       new Function("obj",
+                                       fn = new Function("obj",
                                                "var p=[],print=function(){p.push.apply(p,arguments);};" +
 
                                                // Introduce the data as local variables using with(){}
                                                "');}return p.join('');"
                                        );
 
+                                       // Cache the result
+                                       templateStringCache[str] = fn;
+                               }
+
                                // Provide some basic currying to the user
                                return data ? fn(data) : fn;
                        }