Skip to content

Commit 936082d

Browse files
committed
Fix cache key
1 parent 3bfcfb7 commit 936082d

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

README.md

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -174,43 +174,42 @@ node:
174174
```log
175175
> node --expose-gc ./misc/benchmark.js
176176
177-
clk: ~3.04 GHz
177+
clk: ~3.03 GHz
178178
cpu: Apple M1
179179
runtime: node 20.10.0 (arm64-darwin)
180180
181181
benchmark avg (min … max) p75 / p99 (min top 1%)
182182
------------------------------------------------- -------------------------------
183-
micro-template 32.37 µs/iter 30.04 µs
184-
(28.37 µs … 353.13 µs) 139.67 µs
185-
( 5.95 kb … 1.03 mb) 159.39 kb █▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
183+
micro-template 24.54 µs/iter 23.04 µs
184+
(21.83 µs … 230.92 µs) 67.67 µs
185+
( 56.00 b … 361.68 kb) 146.04 kb ██▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
186186
187-
micro-template (template.variable) 32.74 µs/iter 30.38 µs
188-
(28.75 µs … 406.79 µs) 100.25 µs
189-
( 4.70 kb … 1.91 mb) 159.97 kb █▅▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
187+
micro-template (template.variable) 24.65 µs/iter 24.65 µs
188+
(24.52 µs … 25.17 µs) 24.75 µs ▅▅█ ▅█ ▅ ▅▅
189+
(945.85 b … 952.58 b) 946.55 b ███▁▁▁██▁█▁██▁▁▁▁▁▁▁
190190
191-
ejs.render pre compiled 340.85 µs/iter 342.25 µs
192-
(331.21 µs … 615.62 µs) 380.46 µs
193-
( 8.54 kb … 645.10 kb) 70.75 kb ▃██▆▄▃▃▂▂▃▂▂▃▂▁▁▁▁▁▁▁
191+
ejs.render pre compiled 342.25 µs/iter 347.42 µs
192+
(328.13 µs … 617.17 µs) 384.12 µs
193+
( 11.10 kb … 645.10 kb) 72.46 kb ▂███▆▆▄▄▅▄▃▄▃▂▂▂▂▁▁▁▁
194194
195-
John Resig's tmpl 219.90 µs/iter 222.17 µs
196-
(207.04 µs … 433.33 µs) 259.62 µs
197-
( 28.23 kb … 401.88 kb) 101.18 kb ▃▆██▇▃▃▂▃▃▄▃▃▃▂▁▂▁▁▁▁
195+
John Resig's tmpl 222.23 µs/iter 229.08 µs
196+
(207.71 µs … 428.33 µs) 267.04 µs
197+
( 26.91 kb … 438.41 kb) 101.27 kb ▃▄██▃▃▃▃▄▄▄▃▂▂▁▁▁▁▁▁▁
198198
199199
┌ ┐
200-
micro-template ┤ 32.37 s
201-
micro-template (template.variable) ┤ 32.74 s
202-
ejs.render pre compiled ┤■■■■■■■■■■■■■■■■■■■■■s ■■■■■■■■■■■■ 340.85 µ
203-
John Resig's tmpl ┤■■■■■■■■■■■■■■ s■■■■■■ 219.90 µ
200+
micro-template ┤ 24.54 s
201+
micro-template (template.variable) ┤ 24.65 s
202+
ejs.render pre compiled ┤■■■■■■■■■■■■■■■■■■■■■s ■■■■■■■■■■■■ 342.25 µ
203+
John Resig's tmpl ┤■■■■■■■■■■■■■■ s■■■■■■ 222.23 µ
204204
└ ┘
205205
206206
summary
207207
micro-template
208-
1.01x faster than micro-template (template.variable)
209-
6.79x faster than John Resig's tmpl
210-
10.53x faster than ejs.render pre compiled
208+
1x faster than micro-template (template.variable)
209+
9.06x faster than John Resig's tmpl
210+
13.95x faster than ejs.render pre compiled
211211
```
212212

213-
214213
LICENSE
215214
-------
216215

lib/micro-template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
const template = function (id, data) {
66
if (arguments.length < 2) throw new Error('template() must be called with (template, data)');
7-
const me = template, keys = Object.keys(data || {}), key = [id, keys];
7+
const me = template, keys = Object.keys(data || {}), key = `data:${id}:${keys.join(':')}`;
88
if (!me.cache.has(key)) me.cache.set(key, (function () {
99
let name = id, string = /^[\w\-]+$/.test(id) ? me.get(id): (name = 'template(string)', id); // no warnings
1010
let line = 1;

test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ test('wrapper nested', (t) => {
225225
});
226226

227227
test('include/wrapper with missing args', (t) => {
228+
template.cache.clear();
228229
const origGet = template.get;
229230
template.get = id => id === 'inc' ? '<% include("B") %>' : '';
230231
const r = extended('inc', {});

0 commit comments

Comments
 (0)