Genearte .zig verions of templates to use in production.
This commit is contained in:
91
src/tests/benchmarks/pugjs/bench.js
Normal file
91
src/tests/benchmarks/pugjs/bench.js
Normal file
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Pug.js Benchmark - Comparison with Pugz
|
||||
*
|
||||
* Run: npm install && npm run bench
|
||||
*
|
||||
* Both Pug.js and Pugz benchmarks read from the same files:
|
||||
* ../templates/*.pug (templates)
|
||||
* ../templates/*.json (data)
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const pug = require('pug');
|
||||
|
||||
const iterations = 2000;
|
||||
const templatesDir = path.join(__dirname, '..', 'templates');
|
||||
|
||||
const benchmarks = [
|
||||
'simple-0',
|
||||
'simple-1',
|
||||
'simple-2',
|
||||
'if-expression',
|
||||
'projects-escaped',
|
||||
'search-results',
|
||||
'friends',
|
||||
];
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// Load templates and data from shared files BEFORE benchmarking
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
console.log("");
|
||||
console.log("Loading templates and data...");
|
||||
|
||||
const templates = {};
|
||||
const data = {};
|
||||
|
||||
for (const name of benchmarks) {
|
||||
templates[name] = fs.readFileSync(path.join(templatesDir, `${name}.pug`), 'utf8');
|
||||
data[name] = JSON.parse(fs.readFileSync(path.join(templatesDir, `${name}.json`), 'utf8'));
|
||||
}
|
||||
|
||||
// Compile all templates BEFORE benchmarking
|
||||
const compiled = {};
|
||||
for (const name of benchmarks) {
|
||||
compiled[name] = pug.compile(templates[name], { pretty: true });
|
||||
}
|
||||
|
||||
console.log("Templates compiled. Starting benchmark...\n");
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// Benchmark (Best of 5 runs)
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
console.log("╔═══════════════════════════════════════════════════════════════╗");
|
||||
console.log(`║ Pug.js Benchmark (${iterations} iterations, best of 5) ║`);
|
||||
console.log("║ Templates: benchmarks/templates/*.pug ║");
|
||||
console.log("║ Data: benchmarks/templates/*.json ║");
|
||||
console.log("╚═══════════════════════════════════════════════════════════════╝");
|
||||
|
||||
const runs = 5;
|
||||
let total = 0;
|
||||
|
||||
for (const name of benchmarks) {
|
||||
const compiledFn = compiled[name];
|
||||
const templateData = data[name];
|
||||
|
||||
// Warmup
|
||||
for (let i = 0; i < 100; i++) {
|
||||
compiledFn(templateData);
|
||||
}
|
||||
|
||||
// Run 5 times and take best
|
||||
let bestMs = Infinity;
|
||||
for (let run = 0; run < runs; run++) {
|
||||
const start = process.hrtime.bigint();
|
||||
for (let i = 0; i < iterations; i++) {
|
||||
compiledFn(templateData);
|
||||
}
|
||||
const end = process.hrtime.bigint();
|
||||
const ms = Number(end - start) / 1_000_000;
|
||||
if (ms < bestMs) bestMs = ms;
|
||||
}
|
||||
|
||||
total += bestMs;
|
||||
console.log(` ${name.padEnd(20)} => ${bestMs.toFixed(1).padStart(7)}ms`);
|
||||
}
|
||||
|
||||
console.log("");
|
||||
console.log(` ${"TOTAL".padEnd(20)} => ${total.toFixed(1).padStart(7)}ms`);
|
||||
console.log("");
|
||||
Reference in New Issue
Block a user