Skip to content

Commit e42f58d

Browse files
committed
Add benchmark for cached template usage
1 parent 95cb084 commit e42f58d

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

test/perf.test.mjs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,38 @@ describe('performance', () => {
4242
const elapsed = now - start;
4343
const hz = count / elapsed * 1000;
4444
// eslint-disable-next-line no-console
45-
console.log(`Creation: ${hz|0}/s, average: ${elapsed/count.toFixed(4)}ms`);
45+
console.log(`Creation: ${(hz|0).toLocaleString()}/s, average: ${elapsed/count*1000|0}µs`);
4646
expect(elapsed).toBeGreaterThan(999);
4747
expect(hz).toBeGreaterThan(1000);
4848
});
49+
50+
test('usage', () => {
51+
const results = [];
52+
const Foo = ({ name }) => html`<div class="foo">${name}</div>`;
53+
const statics = [
54+
'\n<div id=app data-loading="true">\n\t<h1>Hello World</h1>\n\t<ul class="items" id=', '>\n\t',
55+
'\n\t</ul>\n\t\n\t<', ' name="foo" />\n\t<', ' name="other">content</', '>\n\n</div>'
56+
];
57+
let count = 0;
58+
function go(count) {
59+
return html(
60+
statics,
61+
`id${count}`,
62+
html(['<li data-id="','">', '</li>'], 'i'+count, 'some text #'+count),
63+
Foo, Foo, Foo
64+
);
65+
}
66+
let now = performance.now();
67+
const start = now;
68+
while ((now = performance.now()) < start+1000) {
69+
count++;
70+
if (results.push(String(go(count)))===10) results.length = 0;
71+
}
72+
const elapsed = now - start;
73+
const hz = count / elapsed * 1000;
74+
// eslint-disable-next-line no-console
75+
console.log(`Usage: ${(hz|0).toLocaleString()}/s, average: ${elapsed/count*1000|0}µs`);
76+
expect(elapsed).toBeGreaterThan(999);
77+
expect(hz).toBeGreaterThan(100000);
78+
});
4979
});

0 commit comments

Comments
 (0)