Skip to content

Commit 8bdbef9

Browse files
committed
update readme
1 parent 629c24f commit 8bdbef9

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

README.md

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,16 @@ SYNTAX
8383
DESCRIPTION
8484
-----------
8585

86-
### template(id or source , data)
86+
### template(id or source , data | array )
8787

8888
If the first argument of template matches `/^[\w\-]+$/`, it is treated as `id` of template. In this case, use `document.getElementById(id).innerHTML` to get source.
8989

9090
Otherwise, the first argument is treated as source directly.
9191

92+
If the second argument is an **Array**, it is treated as a list of property names for the data object. In this case, the template function will be compiled with these property names as its local variables, and the compiled function itself will be returned (not executed). This allows you to precompile a template for repeated use with the same set of variable names.
93+
94+
If the second argument is an **Object**, the template will be rendered immediately using the object's properties as local variables.
95+
9296

9397
CUSTOM `get` FUNCTION
9498
---------------------
@@ -107,16 +111,19 @@ DEFINE DATA VARIABLE SCOPE
107111

108112
micro-template now always expands data variables as local variables in the template function. The template API only supports two arguments: the template source/id and the data object. All keys of the data object are available as local variables in the template code.
109113

114+
If the second argument is an **Array**, it is treated as a list of property names for the data object. In this case, the template function will be compiled with these property names as its local variables, and the function itself will be returned (not executed). This allows you to precompile a template for repeated use with the same set of variable names.
115+
110116
For example:
111117

112118
```js
113-
const result = template('aaa <% foo %> bbb', { foo: 'bar' });
119+
const render = template('aaa <% foo %> bbb', ['foo']);
120+
const result = render({ foo: 'bar' });
114121
// result: 'aaa bar bbb'
115122
```
116123

117124
You can access all properties of the data object directly as variables inside the template.
118125

119-
**Note:** The previous API that allowed calling `template(tmpl)` to return a function is removed. Always use the two-argument form: `template(tmpl, data)`.
126+
**Note:** The previous API that allowed calling `template(tmpl)` to return a function is removed. Always use the two-argument form: `template(tmpl, data)` or `template(tmpl, [prop1, prop2, ...])`.
120127

121128
EXTENDED FEATURES
122129
-----------------
@@ -169,7 +176,7 @@ BENCHMARK
169176
---------
170177

171178
online:
172-
[benchmark on perf.link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MjgxOCwxMjYzNiwxMzYzNiwxMjM2MywxMjgxOCwxNDE4MV0sIm9wcyI6MTI2OTJ9LHsibmFtZSI6ImVqcyIsImNvZGUiOiJjb21waWxlZEVqcyh7IG4gOiAxMDAwIH0pOyIsInJ1bnMiOls5MDksOTA5LDkwOSw4MTgsOTA5LDkwOSw5MDksOTA5LDkwOSw4MTgsOTA5LDkwOSw4MTgsOTA5LDkwOSw5MDksOTA5LDkwOSw5MDksODE4LDgxOCw5MDksOTA5LDkwOSw5MDksOTA5LDkwOSw4MTgsODE4LDgxOCw3MjcsNzI3LDkwOSw5MDksOTA5LDkwOSw5MDksODE4LDkwOSw5MDksOTA5LDkwOSw5MDksOTA5LDkwOSw5MDksOTA5LDkwOSw5MDksOTA5LDkwOSw5MDksOTA5LDkwOSw5MDksOTA5LDkwOSw5MDksOTA5LDkwOSw4MTgsOTA5LDgxOCw4MTgsOTA5LDgxOCw4MTgsOTA5LDkwOSw5MDksOTA5LDkwOSw4MTgsOTA5LDkwOSw4MTgsOTA5LDkwOSw5MDksODE4LDkwOSw5MDksOTA5LDgxOCw5MDldLCJvcHMiOjg4N31dLCJ1cGRhdGVkIjoiMjAyNS0wNi0xMFQxMToyMzoyMS43MTlaIn0%3D )
173180

174181
node:
175182

@@ -178,46 +185,53 @@ node:
178185
```
179186
> node --expose-gc ./misc/benchmark.js
180187
181-
clk: ~3.04 GHz
188+
1 = 1 / 2 = 2 / 3 = Fizz / 4 = 4 / 5 = Buzz / 6 = Fizz / 7 = 7 / 8 = 8 / 9 = Fizz / 10 = Buzz / 11 = 11 / 12 = Fizz / 13 = 13 / 14 = 14 / 15 = FizzBuzz / 16 = 16 / 17 = 17 / 18 = Fizz / 19 = 19 / 20 = Buzz / 21 = Fizz / 22 = 22 / 23 = 23 / 24 = Fizz / 25 = Buzz / 26 = 26 / 27 = Fizz / 28 = 28 / 29 = 29 / 30 = FizzBuzz /
189+
clk: ~3.01 GHz
182190
cpu: Apple M1
183191
runtime: node 20.10.0 (arm64-darwin)
184192
185193
benchmark avg (min … max) p75 / p99 (min top 1%)
186194
------------------------------------------------- -------------------------------
187-
micro-template 24.53 µs/iter 23.04 µs
188-
(21.83 µs … 243.04 µs) 65.54 µs
189-
( 80.00 b … 341.16 kb) 146.07 kb ██▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
195+
micro-template 21.13 µs/iter 19.88 µs ▇
196+
(18.79 µs … 343.21 µs) 49.29 µs
197+
( 12.35 kb … 365.21 kb) 108.72 kb ██▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
198+
199+
micro-template pre compiled 20.22 µs/iter 19.98 µs
200+
(19.76 µs … 22.35 µs) 21.46 µs
201+
( 1.92 kb … 1.94 kb) 1.92 kb ▅█▅█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅
190202
191-
micro-template (not cached) 32.47 µs/iter 30.17 µs
192-
(28.75 µs … 333.62 µs) 93.33 µs
193-
( 3.81 kb … 606.79 kb) 159.79 kb █▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
203+
micro-template (not cached) 29.17 µs/iter 27.50 µs
204+
(26.21 µs … 311.92 µs) 58.13 µs
205+
( 2.14 kb … 378.82 kb) 123.25 kb █▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
194206
195-
micro-template (template.variable) 24.58 µs/iter 23.13 µs
196-
(21.92 µs … 216.00 µs) 65.12 µs
197-
( 22.73 kb … 497.89 kb) 146.28 kb ██▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
207+
micro-template (template.variable) 20.91 µs/iter 20.00 µs
208+
(19.12 µs … 219.87 µs) 30.67 µs
209+
( 2.98 kb … 492.50 kb) 108.87 kb ▁█▇▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
198210
199-
ejs.render pre compiled 348.73 µs/iter 352.79 µs
200-
(332.54 µs … 574.04 µs) 393.13 µs
201-
( 5.10 kb … 613.10 kb) 72.51 kb ▂▂███▆▅▄▄▄▃▄▃▂▂▂▂▂▁▁▁
211+
ejs.render pre compiled 430.88 µs/iter 432.54 µs
212+
(421.38 µs … 642.92 µs) 477.00 µs
213+
( 18.84 kb … 830.84 kb) 99.63 kb ███▇▆▄▃▃▂▂▂▂▂▁▂▁▁▁▁▁▁
202214
203-
John Resig's tmpl 226.28 µs/iter 231.58 µs
204-
(214.42 µs … 436.08 µs) 268.42 µs
205-
( 18.88 kb … 442.88 kb) 101.19 kb ████▆▄▄▅▆▅▄▄▃▂▂▂▁▁▁▁▁
215+
John Resig's tmpl 239.00 µs/iter 238.17 µs
216+
(232.62 µs … 424.21 µs) 274.75 µs
217+
( 33.55 kb … 470.89 kb) 126.63 kb ███▄▂▃▂▂▂▁▁▁▁▁▁▁▁▁▁
206218
207219
┌ ┐
208-
micro-template ┤ 24.53 s
209-
micro-template (not cached) ┤■ 32.47sµ
210-
micro-template (template.variable) ┤ 24.58 s
211-
ejs.render pre compiled ┤■■■■■■■■■■■■■■■■■■■■■s ■■■■■■■■■■■■ 348.73 µ
212-
John Resig's tmpl ┤■■■■■■■■■■■■■■ s■■■■■■ 226.28 µ
220+
micro-template ┤ 21.13 s
221+
micro-template pre compiled ┤ 20.22 s
222+
micro-template (not cached) ┤■ 29.17sµ
223+
micro-template (template.variable) ┤ 20.91 s
224+
ejs.render pre compiled ┤■■■■■■■■■■■■■■■■■■■■■s ■■■■■■■■■■■■ 430.88 µ
225+
John Resig's tmpl ┤■■■■■■■■■■■■■s ■■■■ 239.00 µ
213226
└ ┘
214227
215228
summary
216-
micro-template
217-
1x faster than micro-template (template.variable)
218-
1.32x faster than micro-template (not cached)
219-
9.22x faster than John Resig's tmpl
220-
14.22x faster than ejs.render pre compiled
229+
micro-template pre compiled
230+
1.03x faster than micro-template (template.variable)
231+
1.05x faster than micro-template
232+
1.44x faster than micro-template (not cached)
233+
11.82x faster than John Resig's tmpl
234+
21.31x faster than ejs.render pre compiled
221235
```
222236

223237
LICENSE

0 commit comments

Comments
 (0)