Skip to content

Commit

Permalink
Add simple benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed Jul 6, 2024
1 parent bfe4c9e commit 166e454
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 32 deletions.
19 changes: 15 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 20
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm run unit
benchmarks:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm run benchmarks
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 20
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm run coverage
Expand All @@ -33,7 +44,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 20
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm run gitdiff:ci
Expand All @@ -43,7 +54,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 20
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm run lint
72 changes: 72 additions & 0 deletions benchmarks/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Suite } from "bench-node";
import * as nodeTools from "../src/mjs/index.js";
import * as browserTools from "../src/mjs/browser.js";

const DUMMY_BUFFER = Uint8Array.from([
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde,
0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe,
0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde,
0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
0xbe, 0xef,
]);
const DUMMY_HEX =
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef";

// Note: only include benchmarks for functions with differing implementations for Node and Browser
const BENCHMARKS = [
[
`fromHex`,
(library) => () => {
library.fromHex(DUMMY_HEX);
},
],
[
`toHex`,
(library) => () => {
library.toHex(DUMMY_BUFFER);
},
],
];

const LIBRARIES = [
["Node ", nodeTools],
["Browser", browserTools],
];

function setUpSuite(suite, platform, library, funcName, func) {
suite.add(`${platform} ${funcName}`, func(library));
}

async function main() {
const suite = new Suite();
for (const [funcName, func] of BENCHMARKS) {
for (const [name, library] of LIBRARIES) {
setUpSuite(suite, name, library, funcName, func);
}
}
const results = await suite.run();

const nodeSlower = [];

for (let i = 0; i < results.length; i += 2) {
const nodeResult = results[i];
const browserResult = results[i + 1];
if (nodeResult.opsSec < browserResult.opsSec) {
nodeSlower.push(BENCHMARKS[i / 2][0]);
}
}
if (nodeSlower.length > 0) {
throw new Error(
`\n*** Node was slower in the following:\n *** ${nodeSlower.join(
"\n *** "
)}`
);
}
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
"types": "src/cjs/index.d.ts",
"type": "module",
"scripts": {
"build": "npm run clean && npm run build-ts && npm run convert-cjs && rm -f ./src/cjs/browser.d.ts",
"benchmarks": "node benchmarks/main.js",
"build": "npm run clean && npm run build-ts && npm run convert-cjs",
"build-ts": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json",
"clean": "rm -rf ./src/* && rm -rf ./coverage && rm -f ./package-lock.json",
"convert-cjs": "for f in ./src/cjs/*.js; do mv -- \"$f\" \"${f%.js}.cjs\"; done",
"convert-cjs": "for f in ./src/cjs/*.js; do sed -i 's/\\.js\"/.cjs\"/g' \"$f\"; mv -- \"$f\" \"${f%.js}.cjs\"; done",
"coverage": "npm run unit -- --coverage",
"eslint": "eslint ts_src/*.ts",
"format": "npm run eslint -- --fix",
Expand All @@ -50,6 +51,7 @@
"@types/node": "16.11.1",
"@typescript-eslint/eslint-plugin": "5.0.0",
"@typescript-eslint/parser": "5.0.0",
"bench-node": "0.0.1-beta.0",
"eslint": "8.0.1",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-prettier": "4.0.0",
Expand Down
5 changes: 5 additions & 0 deletions src/cjs/browser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export declare function toUtf8(bytes: Uint8Array): string;
export declare function toHex(bytes: Uint8Array): string;
export declare function fromHex(hexString: string): Uint8Array;
export declare type CompareResult = -1 | 0 | 1;
export declare function compare(v1: Uint8Array, v2: Uint8Array): CompareResult;
13 changes: 4 additions & 9 deletions src/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
function toUtf8(bytes) {
return Buffer.from(bytes || []).toString();
}
exports.toUtf8 = toUtf8;
exports.toUtf8 = exports.compare = exports.fromHex = exports.toHex = void 0;
function toHex(bytes) {
return Buffer.from(bytes || []).toString("hex");
}
Expand All @@ -13,7 +9,6 @@ function fromHex(hexString) {
return Uint8Array.from(Buffer.from(hexString || "", "hex"));
}
exports.fromHex = fromHex;
function compare(v1, v2) {
return Buffer.from(v1).compare(Buffer.from(v2));
}
exports.compare = compare;
var browser_js_1 = require("./browser.cjs");
Object.defineProperty(exports, "compare", { enumerable: true, get: function () { return browser_js_1.compare; } });
Object.defineProperty(exports, "toUtf8", { enumerable: true, get: function () { return browser_js_1.toUtf8; } });
4 changes: 1 addition & 3 deletions src/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export declare function toUtf8(bytes: Uint8Array): string;
export declare function toHex(bytes: Uint8Array): string;
export declare function fromHex(hexString: string): Uint8Array;
export declare type CompareResult = -1 | 0 | 1;
export declare function compare(v1: Uint8Array, v2: Uint8Array): CompareResult;
export { compare, CompareResult, toUtf8 } from './browser.js';
7 changes: 1 addition & 6 deletions src/mjs/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
export function toUtf8(bytes) {
return Buffer.from(bytes || []).toString();
}
export function toHex(bytes) {
return Buffer.from(bytes || []).toString("hex");
}
export function fromHex(hexString) {
return Uint8Array.from(Buffer.from(hexString || "", "hex"));
}
export function compare(v1, v2) {
return Buffer.from(v1).compare(Buffer.from(v2));
}
export { compare, toUtf8 } from './browser.js';
9 changes: 1 addition & 8 deletions ts_src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export function toUtf8(bytes: Uint8Array): string {
return Buffer.from(bytes || []).toString();
}

export function toHex(bytes: Uint8Array): string {
return Buffer.from(bytes || []).toString("hex");
}
Expand All @@ -10,7 +6,4 @@ export function fromHex(hexString: string): Uint8Array {
return Uint8Array.from(Buffer.from(hexString || "", "hex"));
}

export type CompareResult = -1 | 0 | 1;
export function compare(v1: Uint8Array, v2: Uint8Array): CompareResult {
return Buffer.from(v1).compare(Buffer.from(v2)) as CompareResult;
}
export { compare, CompareResult, toUtf8 } from './browser.js';

Check failure on line 9 in ts_src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `'./browser.js'` with `"./browser.js"`

0 comments on commit 166e454

Please sign in to comment.