Skip to content

Commit

Permalink
perf: reduce JUMP bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Sep 30, 2024
1 parent abc4400 commit 9b8919d
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 277 deletions.
6 changes: 2 additions & 4 deletions bench/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* eslint-disable no-unused-vars */

import { html } from "../src/index.js";
import { html } from "ghtml";
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`<div>Hello, world!</div>`;
Expand Down
2 changes: 1 addition & 1 deletion bin/example/assets/script.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
globalThis.console.warn("Hello World!");
globalThis.console.log("Hello World!");
4 changes: 1 addition & 3 deletions bin/example/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable n/no-missing-import, require-await */

import { html } from "ghtml";

export default async (fastify) => {
Expand All @@ -21,7 +19,7 @@ export default async (fastify) => {
<body>
!${inner}
</body>
</html> `;
</html>`;
});

fastify.get("/", async (request, reply) => {
Expand Down
11 changes: 3 additions & 8 deletions bin/example/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable n/no-missing-import */

import Fastify from "fastify";

const fastify = Fastify();
Expand All @@ -17,10 +15,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}`);
15 changes: 8 additions & 7 deletions bin/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node

import { generateHashesAndReplace } from "./utils.js";
import process from "node:process";

Expand All @@ -18,7 +19,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);
Expand All @@ -31,22 +32,22 @@ 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,
refs,
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);
}
};
Expand Down
8 changes: 6 additions & 2 deletions bin/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable no-await-in-loop */

import { Glob } from "glob";
import { createHash } from "node:crypto";
import { readFile, writeFile } from "node:fs/promises";
Expand All @@ -8,11 +6,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 "";
}
};
Expand All @@ -26,6 +26,7 @@ const updateFilePathsWithHashes = async (
) => {
for (let ref of refs) {
ref = ref.replaceAll(win32.sep, posix.sep);

if (!ref.endsWith("/")) {
ref += "/";
}
Expand Down Expand Up @@ -84,11 +85,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 += "/";
}
Expand All @@ -114,6 +117,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]);
}
}
Expand Down
13 changes: 12 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import grules from "grules";

export default [...grules];
export default [
...grules,
{
rules: {
"no-await-in-loop": "off",
"require-unicode-regexp": "off",
},
},
{
ignores: ["bin/example/**/*.js"],
},
];
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
"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"
},
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"c8": "^10.1.2",
"grules": "^0.25.0",
"grules": "^0.25.3",
"tinybench": "^2.9.0",
"typescript": ">=5.6.2"
},
Expand Down
Loading

0 comments on commit 9b8919d

Please sign in to comment.