Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Stop using crypto for template cache.

Greatly improves speed when looking up cache. Hopefully doesn't cause memory issues with size of keys in the cache object.
  • Loading branch information...
commit d759a1e8dbe10f2918d04af42e1e520e71f7e9cd 1 parent 944516b
@paularmstrong authored
Showing with 21 additions and 19 deletions.
  1. +3 −6 index.js
  2. +18 −13 tests/speed.js
View
9 index.js
@@ -1,6 +1,5 @@
var fs = require('fs'),
path = require('path'),
- crypto = require('crypto'),
tags = require('./lib/tags'),
parser = require('./lib/parser'),
@@ -117,13 +116,11 @@ exports.fromFile = function (filepath) {
};
exports.fromString = function (string) {
- var hash = crypto.createHash('md5').update(string).digest('hex');
-
- if (!CACHE.hasOwnProperty(hash)) {
- CACHE[hash] = createTemplate(string, hash);
+ if (!CACHE.hasOwnProperty(string)) {
+ CACHE[string] = createTemplate(string, string);
}
- return CACHE[hash];
+ return CACHE[string];
};
exports.compile = function (source, options, callback) {
View
31 tests/speed.js
@@ -1,5 +1,5 @@
var template = require('../index'),
- tplF, tplS, array, output, d, i;
+ tplF, tplS, array, output, d, i, tplString;
console.log();
console.log('Starting speed tests...');
@@ -8,18 +8,23 @@ template.init({
root: __dirname + '/templates'
});
-tplS = template.fromString(
- "{% for v in array %}"
- + "{% if 1 %}"
- + "{% for k in v %}"
- + "\n{{forloop.index}} {{k}}: "
- + "{% if forloop.index in 'msafas' %}"
- + "<p>Hello World {{k}}{{foo}}{{k}}{{foo}}{{k}}{{foo}}</p>"
- + "{% endif %}"
- + "{% endfor %}"
- + "{% endif %}"
- + "{% endfor %}"
-);
+tplString = "{% for v in array %}"
++ "{% if 1 %}"
++ "{% for k in v %}"
++ "\n{{forloop.index}} {{k}}: "
++ "{% if forloop.index in 'msafas' %}"
++ "<p>Hello World {{k}}{{foo}}{{k}}{{foo}}{{k}}{{foo}}</p>"
++ "{% endif %}"
++ "{% endfor %}"
++ "{% endif %}"
++ "{% endfor %}";
+
+console.time('Compile Template');
+i = 1000;
+while (i--) {
+ tplS = template.fromString(tplString);
+}
+console.timeEnd('Compile Template');
array = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], { af: "s", baz: "d", d: "f" }, "zeus"];
tplF = template.fromFile("include_base.html");
Please sign in to comment.
Something went wrong with that request. Please try again.