From a9e471d5e32c28de4be68879391e3fe474a5ee7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sun, 29 Sep 2024 16:13:14 +0200 Subject: [PATCH] perf: reduce JUMP bytecode --- bench/index.js | 4 +--- bin/example/assets/script.js | 2 +- bin/example/routes/index.js | 3 +-- bin/example/server.js | 10 +++------- bin/src/index.js | 14 +++++++------- bin/src/utils.js | 8 +++++++- package.json | 4 ++-- src/html.js | 4 +--- 8 files changed, 23 insertions(+), 26 deletions(-) diff --git a/bench/index.js b/bench/index.js index 321f573..52abd6a 100644 --- a/bench/index.js +++ b/bench/index.js @@ -1,13 +1,11 @@ /* eslint-disable no-unused-vars */ - import { html } from "../src/index.js"; import { Bench } from "tinybench"; import { writeFileSync } from "node:fs"; import { Buffer } from "node:buffer"; -let result = ""; - const bench = new Bench({ time: 500 }); +let result = ""; bench.add("simple HTML formatting", () => { result = html`
Hello, world!
`; diff --git a/bin/example/assets/script.js b/bin/example/assets/script.js index cdf6b44..ed212d9 100644 --- a/bin/example/assets/script.js +++ b/bin/example/assets/script.js @@ -1 +1 @@ -globalThis.console.warn("Hello World!"); +globalThis.console.log("Hello World!"); diff --git a/bin/example/routes/index.js b/bin/example/routes/index.js index 041ae89..85b08c8 100644 --- a/bin/example/routes/index.js +++ b/bin/example/routes/index.js @@ -1,5 +1,4 @@ /* eslint-disable n/no-missing-import, require-await */ - import { html } from "ghtml"; export default async (fastify) => { @@ -21,7 +20,7 @@ export default async (fastify) => { !${inner} - `; + `; }); fastify.get("/", async (request, reply) => { diff --git a/bin/example/server.js b/bin/example/server.js index 0f2e03d..0870d2e 100644 --- a/bin/example/server.js +++ b/bin/example/server.js @@ -1,5 +1,4 @@ /* eslint-disable n/no-missing-import */ - import Fastify from "fastify"; const fastify = Fastify(); @@ -17,10 +16,7 @@ await fastify.register(import("@fastify/static"), { // Routes fastify.register(import("./routes/index.js")); -fastify.listen({ port: 5050 }, (err, address) => { - if (err) { - throw err; - } +// Listen +const address = await fastify.listen({ port: 5050 }); - globalThis.console.warn(`Server listening at ${address}`); -}); +globalThis.console.log(`Server listening at ${address}`); diff --git a/bin/src/index.js b/bin/src/index.js index bdebf92..d771402 100755 --- a/bin/src/index.js +++ b/bin/src/index.js @@ -18,7 +18,7 @@ const parseArguments = (args) => { } if (!roots || !refs) { - globalThis.console.error( + globalThis.console.log( 'Usage: npx ghtml --roots="base/path/to/scan/assets/1/,base/path/to/scan/assets/2/" --refs="views/path/to/append/hashes/1/,views/path/to/append/hashes/2/" [--prefix="/optional/prefix/"]', ); process.exit(1); @@ -31,10 +31,10 @@ const main = async () => { const { roots, refs, prefix } = parseArguments(process.argv.slice(2)); try { - globalThis.console.warn(`Generating hashes and updating file paths...`); - globalThis.console.warn(`Scanning files in: ${roots}`); - globalThis.console.warn(`Updating files in: ${refs}`); - globalThis.console.warn(`Using prefix: ${prefix}`); + globalThis.console.log(`Generating hashes and updating file paths...`); + globalThis.console.log(`Scanning files in: ${roots}`); + globalThis.console.log(`Updating files in: ${refs}`); + globalThis.console.log(`Using prefix: ${prefix}`); await generateHashesAndReplace({ roots, @@ -42,11 +42,11 @@ const main = async () => { prefix, }); - globalThis.console.warn( + globalThis.console.log( "Hash generation and file updates completed successfully.", ); } catch (error) { - globalThis.console.error(`Error occurred: ${error.message}`); + globalThis.console.log(`Error occurred: ${error.message}`); process.exit(1); } }; diff --git a/bin/src/utils.js b/bin/src/utils.js index c538e3d..17023a5 100644 --- a/bin/src/utils.js +++ b/bin/src/utils.js @@ -1,5 +1,4 @@ /* eslint-disable no-await-in-loop */ - import { Glob } from "glob"; import { createHash } from "node:crypto"; import { readFile, writeFile } from "node:fs/promises"; @@ -8,11 +7,13 @@ import { win32, posix } from "node:path"; const generateFileHash = async (filePath) => { try { const fileBuffer = await readFile(filePath); + return createHash("md5").update(fileBuffer).digest("hex").slice(0, 16); } catch (err) { if (err.code !== "ENOENT") { throw err; } + return ""; } }; @@ -26,6 +27,7 @@ const updateFilePathsWithHashes = async ( ) => { for (let ref of refs) { ref = ref.replaceAll(win32.sep, posix.sep); + if (!ref.endsWith("/")) { ref += "/"; } @@ -84,11 +86,13 @@ export const generateHashesAndReplace = async ({ skipPatterns = ["**/node_modules/**"], }) => { const fileHashes = new Map(); + roots = Array.isArray(roots) ? roots : [roots]; refs = Array.isArray(refs) ? refs : [refs]; for (let root of roots) { root = root.replaceAll(win32.sep, posix.sep); + if (!root.endsWith("/")) { root += "/"; } @@ -106,6 +110,7 @@ export const generateHashesAndReplace = async ({ for await (let filePath of filesIterable) { filePath = filePath.replaceAll(win32.sep, posix.sep); + queue.push(generateFileHash(filePath)); files.push(filePath); } @@ -114,6 +119,7 @@ export const generateHashesAndReplace = async ({ for (let i = 0; i < files.length; ++i) { const fileRelativePath = posix.relative(root, files[i]); + fileHashes.set(fileRelativePath, hashes[i]); } } diff --git a/package.json b/package.json index 3b19b57..c4d43dd 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "test": "npm run lint && c8 --100 node --test test/*.js", "lint": "eslint . && prettier --check .", "lint:fix": "eslint --fix . && prettier --write .", - "typescript": "tsc src/*.js --allowJs --declaration --emitDeclarationOnly" + "typescript": "tsc src/*.js --allowJs --declaration --emitDeclarationOnly --skipLibCheck" }, "dependencies": { "glob": "^10.4.5" @@ -27,7 +27,7 @@ "devDependencies": { "@fastify/pre-commit": "^2.1.0", "c8": "^10.1.2", - "grules": "^0.25.0", + "grules": "^0.25.1", "tinybench": "^2.9.0", "typescript": ">=5.6.2" }, diff --git a/src/html.js b/src/html.js index 003851d..95ac4d0 100644 --- a/src/html.js +++ b/src/html.js @@ -1,5 +1,4 @@ /* eslint-disable no-await-in-loop, require-unicode-regexp */ - const escapeRegExp = /["&'<=>]/g; const escapeFunction = (string) => { @@ -25,9 +24,8 @@ const escapeFunction = (string) => { case 61: // = escaped += string.slice(start, i) + "="; break; - case 62: // > + default: // > escaped += string.slice(start, i) + ">"; - break; } start = escapeRegExp.lastIndex;