|
| 1 | +/** |
| 2 | + * Copyright 2018 Google Inc. All Rights Reserved. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +import htm from '../src/index.mjs'; |
| 15 | + |
| 16 | +const h = (tag, props, ...children) => ({ tag, props, children }); |
| 17 | +const html = htm.bind(h); |
| 18 | + |
| 19 | +describe('performance', () => { |
| 20 | + test('creation', () => { |
| 21 | + const results = []; |
| 22 | + const Foo = ({ name }) => html`<div class="foo">${name}</div>`; |
| 23 | + const statics = [ |
| 24 | + '\n<div id=app data-loading="true">\n\t<h1>Hello World</h1>\n\t<ul class="items" id=', '>\n\t', |
| 25 | + '\n\t</ul>\n\t\n\t<', ' name="foo" />\n\t<', ' name="other">content</', '>\n\n</div>' |
| 26 | + ]; |
| 27 | + let count = 0; |
| 28 | + function go(count) { |
| 29 | + return html( |
| 30 | + statics.concat(['count:', count]), |
| 31 | + `id${count}`, |
| 32 | + html(['<li data-id="','">', '</li>'], 'i'+count, 'some text #'+count), |
| 33 | + Foo, Foo, Foo |
| 34 | + ); |
| 35 | + } |
| 36 | + let now = performance.now(); |
| 37 | + const start = now; |
| 38 | + while ((now = performance.now()) < start+1000) { |
| 39 | + count++; |
| 40 | + if (results.push(String(go(count)))===10) results.length = 0; |
| 41 | + } |
| 42 | + const elapsed = now - start; |
| 43 | + const hz = count / elapsed * 1000; |
| 44 | + // eslint-disable-next-line no-console |
| 45 | + console.log(`Creation: ${hz|0}/s, average: ${elapsed/count.toFixed(4)}ms`); |
| 46 | + expect(elapsed).toBeGreaterThan(999); |
| 47 | + expect(hz).toBeGreaterThan(1000); |
| 48 | + }); |
| 49 | +}); |
0 commit comments