Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
* limitations under the License.
*/

const CACHE = {};
const CACHE = new WeakMap();

const stringify = JSON.stringify;

export default function html(statics) {
let key = '.';
for (let i=0; i<statics.length; i++) key += statics[i].length + ',' + statics[i];
const tpl = CACHE[key] || (CACHE[key] = build(statics));
let tpl = CACHE.get(statics);
if (!tpl) {
CACHE.set(statics, tpl = build(statics));
}

// eslint-disable-next-line prefer-rest-params
return tpl(this, arguments);
Expand Down
22 changes: 11 additions & 11 deletions test/perf.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('performance', () => {
return html(
statics.concat(['count:', count]),
`id${count}`,
html(['<li data-id="','">', '</li>'], 'i'+count, 'some text #'+count),
html`<li data-id="${'i'+count}">${'some text #'+count}</li>`,
Foo, Foo, Foo
);
}
Expand All @@ -50,18 +50,18 @@ describe('performance', () => {
test('usage', () => {
const results = [];
const Foo = ({ name }) => html`<div class="foo">${name}</div>`;
const statics = [
'\n<div id=app data-loading="true">\n\t<h1>Hello World</h1>\n\t<ul class="items" id=', '>\n\t',
'\n\t</ul>\n\t\n\t<', ' name="foo" />\n\t<', ' name="other">content</', '>\n\n</div>'
];
let count = 0;
function go(count) {
return html(
statics,
`id${count}`,
html(['<li data-id="','">', '</li>'], 'i'+count, 'some text #'+count),
Foo, Foo, Foo
);
return html`
<div id=app data-loading="true">
<h1>Hello World</h1>
<ul class="items" id=${'id' + count}>
${html`<li data-id="${'i' + count}">${'some text #' + count}</li>`}
</ul>
<${Foo} name="foo" />
<${Foo} name="other">content<//>
</div>
`;
}
let now = performance.now();
const start = now;
Expand Down