Skip to content

Commit 13c3123

Browse files
committed
Create cache key by run-length encoding statics
Prefix the key with a non-numeric character, as this seems to give a speed-up on Chrome.
1 parent ac0655f commit 13c3123

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/index.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const CACHE = {};
1616
const stringify = JSON.stringify;
1717

1818
export default function html(statics) {
19-
const key = stringify(statics);
19+
let key = '.';
20+
for (let i=0; i<statics.length; i++) key += statics[i].length + ',' + statics[i];
2021
const tpl = CACHE[key] || (CACHE[key] = build(statics));
2122

2223
// eslint-disable-next-line prefer-rest-params

test/index.test.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,7 @@ describe('htm', () => {
164164
test('cache key should be unique', () => {
165165
html`<a b="${'foo'}" />`;
166166
expect(html`<a b="\0" />`).toEqual(h('a', { b: '\0' }));
167+
expect(html`<a>${''}9aaaaaaaaa${''}</a>`).not.toEqual(html`<a>${''}0${''}aaaaaaaaa${''}</a>`);
168+
expect(html`<a>${''}0${''}aaaaaaaa${''}</a>`).not.toEqual(html`<a>${''}.8aaaaaaaa${''}</a>`);
167169
});
168170
});

0 commit comments

Comments
 (0)