Skip to content

Commit d0193a2

Browse files
authored
Merge pull request #16 from cho45/benchmark-issue
not cached なベンチが不当に早く計測されている
2 parents 67a9711 + c6cf9ca commit d0193a2

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

README.md

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -187,53 +187,58 @@ node:
187187
> node --expose-gc ./misc/benchmark.js
188188
189189
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 /
190-
clk: ~3.01 GHz
190+
clk: ~3.05 GHz
191191
cpu: Apple M1
192192
runtime: node 20.10.0 (arm64-darwin)
193193
194-
benchmark avg (min … max) p75 / p99 (min top 1%)
194+
benchmark avg (min … max) p75 / p99 (min top 1%)
195195
------------------------------------------------- -------------------------------
196-
micro-template 21.13 µs/iter 19.88 µs
197-
(18.79 µs … 343.21 µs) 49.29 µs
198-
( 12.35 kb … 365.21 kb) 108.72 kb ██▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
196+
micro-template 20.72 µs/iter 19.75 µs
197+
(18.83 µs … 232.63 µs) 35.50 µs
198+
( 11.37 kb … 429.21 kb) 108.69 kb ▄█▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
199199
200-
micro-template pre compiled 20.22 µs/iter 19.98 µs
201-
(19.76 µs … 22.35 µs) 21.46 µs
202-
( 1.92 kb … 1.94 kb) 1.92 kb ▅█▅█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅
200+
micro-template pre compiled 19.64 µs/iter 19.65 µs
201+
(19.48 µs … 19.94 µs) 19.83 µs ▅ ▅▅ █▅▅▅▅▅
202+
( 1.92 kb … 1.96 kb) 1.93 kb █▁▁██▁██████▁▁▁▁▁▁▁▁
203203
204-
micro-template (not cached) 29.17 µs/iter 27.50 µs
205-
(26.21 µs … 311.92 µs) 58.13 µs
206-
( 2.14 kb … 378.82 kb) 123.25 kb ██▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
204+
micro-template (not cached) 174.13 µs/iter 172.08 µs ▆█
205+
(164.50 µs … 627.62 µs) 325.83 µs ██
206+
( 4.38 kb … 639.55 kb) 140.32 kb ██▄▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
207207
208-
micro-template (template.variable) 20.91 µs/iter 20.00 µs
209-
(19.12 µs … 219.87 µs) 30.67 µs
210-
( 2.98 kb … 492.50 kb) 108.87 kb ▁█▇▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
208+
micro-template (template.variable) 21.01 µs/iter 20.13 µs
209+
(19.25 µs … 245.96 µs) 32.79 µs
210+
( 12.90 kb … 356.45 kb) 108.75 kb ██▆▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
211211
212-
ejs.render pre compiled 430.88 µs/iter 432.54 µs
213-
(421.38 µs … 642.92 µs) 477.00 µs
214-
( 18.84 kb … 830.84 kb) 99.63 kb ███▇▆▄▃▃▂▂▂▂▂▁▂▁▁▁▁▁▁
212+
ejs.compile (pre compiled) 438.36 µs/iter 443.67 µs ▄█▄
213+
(418.00 µs … 665.54 µs) 489.46 µs ███▂
214+
( 10.37 kb … 798.84 kb) 99.42 kb ▁▁██████▇▅▅▃▃▂▂▂▁▂▁▁▁
215215
216-
John Resig's tmpl 239.00 µs/iter 238.17 µs
217-
(232.62 µs … 424.21 µs) 274.75 µs
218-
( 33.55 kb … 470.89 kb) 126.63 kb ███▄▂▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁
216+
ejs.render 457.18 µs/iter 458.37 µs █
217+
(439.75 µs … 659.54 µs) 520.92 µs
218+
( 5.91 kb … 842.41 kb) 141.02 kb ▃▄██▇▆▄▄▃▃▂▂▂▂▂▁▁▁▁▁▁
219+
220+
John Resig's tmpl 240.02 µs/iter 240.67 µs
221+
(228.29 µs … 436.46 µs) 277.63 µs ▅
222+
( 14.08 kb … 438.89 kb) 126.56 kb ▃▅███▆▄▃▃▂▃▂▂▂▂▁▁▁▁▁▁
219223
220224
┌ ┐
221-
micro-template ┤ 21.13 s
222-
micro-template pre compiled ┤ 20.22 s
223-
micro-template (not cached) ┤■ 29.17sµ
224-
micro-template (template.variable) ┤ 20.91 s
225-
ejs.render pre compiled ┤■■■■■■■■■■■■■■■■■■■■■s ■■■■■■■■■■■■ 430.88 µ
226-
John Resig's tmpl ┤■■■■■■■■■■■■■s ■■■■ 239.00 µ
225+
micro-template ┤ 20.72 s
226+
micro-template pre compiled ┤ 19.64 s
227+
micro-template (not cached) ┤■■■■■■■■■■s ■ 174.13 µ
228+
micro-template (template.variable) ┤ 21.01 s
229+
ejs.compile (pre compiled) ┤■■■■■■■■■■■■■■■■■■■■ s■■■■■■■■■■■■ 438.36 µ
230+
ejs.render ┤■■■■■■■■■■■■■■■■■■■■■s ■■■■■■■■■■■■ 457.18 µ
231+
John Resig's tmpl ┤■■■■■■■■■■■■ s■■■■ 240.02 µ
227232
└ ┘
228233
229234
summary
230-
231235
micro-template pre compiled
232-
1.03x faster than micro-template (template.variable)
233-
1.05x faster than micro-template
234-
1.44x faster than micro-template (not cached)
235-
11.82x faster than John Resig's tmpl
236-
21.31x faster than ejs.render pre compiled
236+
1.06x faster than micro-template
237+
1.07x faster than micro-template (template.variable)
238+
8.87x faster than micro-template (not cached)
239+
12.22x faster than John Resig's tmpl
240+
22.32x faster than ejs.compile (pre compiled)
241+
23.28x faster than ejs.render
237242
```
238243

239244
LICENSE

misc/benchmark.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import assert from 'assert';
55
import { template } from '../lib/micro-template.js';
66
import ejs from 'ejs';
77
import fs from 'fs';
8-
import { bench, run, summary, barplot } from 'mitata';
8+
import { bench, run, summary, barplot, do_not_optimize } from 'mitata';
99

1010
// ============================================================================
1111
// Simple JavaScript Templating
@@ -77,6 +77,7 @@ assert.equal(output1, output2, 'output should be same');
7777
assert.equal(output1, output3, 'output should be same');
7878
assert.equal(output1, output4, 'output should be same');
7979

80+
let counter = 0;
8081
barplot(() => {
8182
summary(() => {
8283
bench('micro-template', () => {
@@ -88,16 +89,19 @@ barplot(() => {
8889
});
8990
bench('micro-template (not cached)', () => {
9091
template.variable = null;
91-
template.cache.clear();
92-
template(fizzbuzz, { n: 300 });
92+
template.cache.clear(); // これだけだと V8 の最適化が走るので、counter で少しずつ変える
93+
template(fizzbuzz + (counter++), { n: 300 });
9394
});
9495
bench('micro-template (template.variable)', () => {
9596
template.variable = 'stash';
9697
template(fizzbuzzVar, { n: 300 });
9798
});
98-
bench('ejs.render pre compiled', () => {
99+
bench('ejs.compile (pre compiled)', () => {
99100
ejsFunc({ n: 300 });
100101
});
102+
bench('ejs.render', () => {
103+
ejs.render(fizzbuzz, { n: 300 });
104+
});
101105
bench("John Resig's tmpl", () => {
102106
tmpl(fizzbuzz, {n : 300 });
103107
});

0 commit comments

Comments
 (0)