minify-html/bench/bench.js

96 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-12-30 03:58:50 -05:00
"use strict";
const fs = require("fs");
const path = require("path");
const benchmark = require("benchmark");
const htmlMinifier = require("html-minifier");
const minimize = require("minimize");
const hyperbuild = require("hyperbuild");
const tests_dir = path.join(__dirname, "tests");
const tests = fs.readdirSync(tests_dir).map(name => ({
name,
content: fs.readFileSync(path.join(tests_dir, name), "utf8"),
}));
const sizes = {};
const setSize = (program, test, result) => {
console.log(`Received result for ${program} - ${test}`);
if (!sizes[test]) {
sizes[test] = {
original: {
result: tests.find(t => t.name === test).content.length,
},
};
}
const original = sizes[test].original.result;
sizes[test][program] = {
result: result,
difference: `${((result - original) / original * 100).toFixed(2)}%`,
};
};
const htmlMinifierSettings = {
caseSensitive: false,
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: true,
collapseWhitespace: true,
conservativeWhitespace: false,
customEventAttributes: [],
decodeEntities: true,
html5: true,
ignoreCustomComments: [],
ignoreCustomFragments: [],
includeAutoGeneratedTags: true,
keepClosingSlash: false,
minifyCSS: false,
minifyJS: false,
minifyURLs: false,
preserveLineBreaks: false,
preventAttributesEscaping: false,
processConditionalComments: true,
processScripts: [],
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: false,
removeEmptyElements: false,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeTagWhitespace: true,
sortAttributes: true,
sortClassName: true,
trimCustomFragments: false,
useShortDoctype: true,
};
new benchmark.Suite()
.add("hyperbuild", () => {
for (const t of tests) {
setSize("hyperbuild", t.name, hyperbuild.minify(Buffer.from(t.content)));
}
})
.add("html-minifier", () => {
for (const t of tests) {
setSize("html-minifier", t.name, htmlMinifier.minify(t.content, htmlMinifierSettings).length);
}
})
.add("minimize", () => {
for (const t of tests) {
setSize("minimize", t.name, new minimize().parse(t.content).length);
}
})
.on('cycle', event => {
console.info(event.target.toString());
})
.on('complete', function () {
console.info(`Fastest is ${this.filter('fastest').map('name')}`);
Object.entries(sizes).forEach(([test, results]) => {
console.info(test);
console.table(results);
});
})
.run({'async': true});