Something went wrong. Try again.
composable JavaScript eval templating
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102import assert from 'node:assert/strict';
import { bench, do_not_optimize, run } from 'mitata';
import { create } from '../src/index.ts';
const code = create();const sizes = [8, 64, 512];
const template = (strings) => Object.assign([...strings], { raw: [...strings] });const concatenate = (parts) => code(template(Array(parts.length + 1).fill('')), ...parts);const wrap = (body) => code`let total = 0; ${body} return total;`;
const chain = (parts) => { let body = code``; for (const part of parts) { body = code`${body}${part}`; } return body;};
const values = (size, { unique }) => { const shared = { value: 1 }; const inputs = Array.from({ length: size }, () => (unique ? { value: 1 } : shared)); const strings = ['total += ', ...Array(size - 1).fill('.value; total += '), '.value;']; return code(template(strings), ...inputs);};
const identifiers = (size, { unique }) => { const shared = code.local('value'); const inputs = []; const strings = ['var ']; for (let index = 0; index < size; index++) { const ident = unique ? code.local('value') : shared; inputs.push(ident, ident); strings.push(' = 1; total += ', index === size - 1 ? ';' : '; var '); } return code(template(strings), ...inputs);};
const cases = { 'all-identifiers/repeated': (size) => identifiers(size, { unique: false }), 'all-identifiers/unique': (size) => identifiers(size, { unique: true }), 'all-values/repeated': (size) => values(size, { unique: false }), 'all-values/unique': (size) => values(size, { unique: true }), 'concatenation/flat': (size) => concatenate(Array.from({ length: size }, () => code.raw('total += 1;'))), mixed: (size) => { const shared = { value: 1 }; return concatenate( Array.from({ length: size }, (_, index) => { const ident = code.local('value'); const value = index % 2 === 0 ? shared : { value: 1 }; return code`const ${ident} = ${value}.value; ${code`total += ${ident};`}`; }), ); }, 'raw-source': (size) => code.raw('total += 1;'.repeat(size)),};
for (const [name, makeBody] of Object.entries(cases)) { bench(`eval/${name}/$size`, function* (state) { const size = state.get('size'); assert.equal(wrap(makeBody(size)).eval(), size); yield { [0]() { return wrap(makeBody(size)); }, bench(input) { return do_not_optimize(input.eval()); }, }; }).args('size', sizes);}
bench('eval/concatenation/deep/$depth', function* (state) { const depth = state.get('depth'); const makeFragment = () => wrap(chain(Array.from({ length: depth }, () => code.raw('total += 1;')))); assert.equal(makeFragment().eval(), depth); yield { [0]() { return makeFragment(); }, bench(input) { return do_not_optimize(input.eval()); }, };}).args('depth', [8, 32, 128]);
bench('construct/concatenation/$size', function* (state) { const size = state.get('size'); yield { [0]() { return Array.from({ length: size }, () => code.raw('total += 1;')); }, bench(input) { return do_not_optimize(chain(input)); }, };}).args('size', sizes);
await run({ filter: new RegExp(process.argv[2] ?? ''), throw: true });