|
| 1 | +/** |
| 2 | + * Mitata benchmark for `@glimmer/syntax` parse (`preprocess`), normalize |
| 3 | + * (ASTv1 → ASTv2 — where the loc-conversion hot path lives), and full |
| 4 | + * `precompile()` (via `ember-template-compiler`, which inlines |
| 5 | + * `@glimmer/compiler`). |
| 6 | + * |
| 7 | + * Run: |
| 8 | + * pnpm build # produces the dist artifacts this bench imports |
| 9 | + * pnpm bench:precompile |
| 10 | + * |
| 11 | + * To compare branches, check out each branch, build, run the bench, and diff |
| 12 | + * the ms/iter numbers. |
| 13 | + * |
| 14 | + * Sizes: |
| 15 | + * small — ~1.5k chars (route-template fragment) |
| 16 | + * medium — small × 3 (~4.5k chars) |
| 17 | + * large — small × 22 (~33k chars, scale of the largest real route |
| 18 | + * templates, e.g. Discourse's admin-user/index.gjs) |
| 19 | + */ |
| 20 | + |
| 21 | +import { bench, do_not_optimize as doNotOptimize, run } from 'mitata'; |
| 22 | + |
| 23 | +/* eslint n/no-missing-import: "off" -- dist/ is built by `pnpm build`; may not exist at lint time */ |
| 24 | +import { normalize, preprocess, src } from '../packages/@glimmer/syntax/dist/es/index.js'; |
| 25 | +import { precompile } from '../dist/prod/packages/ember-template-compiler/index.js'; |
| 26 | + |
| 27 | +const SMALL = `<div class='user-profile {{if this.isPremium "premium"}}'> |
| 28 | + <header class='profile-header'> |
| 29 | + <img src={{this.avatarUrl}} alt={{this.username}} class='avatar' /> |
| 30 | + <h2>{{this.displayName}}</h2> |
| 31 | + <p class='bio'>{{this.bio}}</p> |
| 32 | + {{#if this.isOwnProfile}} |
| 33 | + <button {{on 'click' this.editProfile}}>Edit Profile</button> |
| 34 | + {{/if}} |
| 35 | + </header> |
| 36 | + <nav class='profile-tabs'> |
| 37 | + {{#each this.tabs as |tab|}} |
| 38 | + <button |
| 39 | + class='tab {{if (eq tab.id this.activeTab) "active"}}' |
| 40 | + {{on 'click' (fn this.setTab tab.id)}} |
| 41 | + > |
| 42 | + {{tab.label}}{{#if tab.count}}<span class='count'>{{tab.count}}</span>{{/if}} |
| 43 | + </button> |
| 44 | + {{/each}} |
| 45 | + </nav> |
| 46 | + <section class='profile-content'> |
| 47 | + {{#if (eq this.activeTab 'posts')}} |
| 48 | + {{#each this.posts as |post|}} |
| 49 | + <article class='post-card'> |
| 50 | + <h3>{{post.title}}</h3><p>{{post.excerpt}}</p> |
| 51 | + <footer><time>{{post.createdAt}}</time><span>{{post.views}} views</span></footer> |
| 52 | + </article> |
| 53 | + {{else}} |
| 54 | + <p class='empty-state'>No posts yet.</p> |
| 55 | + {{/each}} |
| 56 | + {{else if (eq this.activeTab 'followers')}} |
| 57 | + {{#each this.followers as |follower|}} |
| 58 | + <div class='follower-card'> |
| 59 | + <img src={{follower.avatar}} alt={{follower.name}} /> |
| 60 | + <span>{{follower.name}}</span> |
| 61 | + <button {{on 'click' (fn this.followUser follower.id)}}> |
| 62 | + {{if follower.isFollowing 'Unfollow' 'Follow'}} |
| 63 | + </button> |
| 64 | + </div> |
| 65 | + {{/each}} |
| 66 | + {{/if}} |
| 67 | + </section> |
| 68 | +</div> |
| 69 | +`; |
| 70 | + |
| 71 | +const FIXTURES = { |
| 72 | + small: SMALL, |
| 73 | + medium: SMALL.repeat(3), |
| 74 | + large: SMALL.repeat(22), |
| 75 | +}; |
| 76 | + |
| 77 | +for (const [size, source] of Object.entries(FIXTURES)) { |
| 78 | + const chars = source.length; |
| 79 | + bench(`parse ${size} (${chars}c)`, () => doNotOptimize(preprocess(source))); |
| 80 | + bench(`normalize ${size} (${chars}c)`, () => doNotOptimize(normalize(new src.Source(source)))); |
| 81 | + bench(`precompile ${size} (${chars}c)`, () => doNotOptimize(precompile(source))); |
| 82 | +} |
| 83 | + |
| 84 | +await run({ throw: true }); |
0 commit comments