Skip to content

Commit 166e454

Browse files
committed
Add simple benchmarks
1 parent bfe4c9e commit 166e454

File tree

8 files changed

+103
-32
lines changed

8 files changed

+103
-32
lines changed

.github/workflows/ci.yml

+15-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,28 @@ jobs:
1313
- uses: actions/checkout@v2
1414
- uses: actions/setup-node@v1
1515
with:
16-
node-version: 14
16+
node-version: 20
1717
registry-url: https://registry.npmjs.org/
1818
- run: npm i
1919
- run: npm run unit
20+
benchmarks:
21+
runs-on: ubuntu-latest
22+
continue-on-error: true
23+
steps:
24+
- uses: actions/checkout@v2
25+
- uses: actions/setup-node@v1
26+
with:
27+
node-version: 20
28+
registry-url: https://registry.npmjs.org/
29+
- run: npm i
30+
- run: npm run benchmarks
2031
coverage:
2132
runs-on: ubuntu-latest
2233
steps:
2334
- uses: actions/checkout@v2
2435
- uses: actions/setup-node@v1
2536
with:
26-
node-version: 14
37+
node-version: 20
2738
registry-url: https://registry.npmjs.org/
2839
- run: npm i
2940
- run: npm run coverage
@@ -33,7 +44,7 @@ jobs:
3344
- uses: actions/checkout@v2
3445
- uses: actions/setup-node@v1
3546
with:
36-
node-version: 14
47+
node-version: 20
3748
registry-url: https://registry.npmjs.org/
3849
- run: npm i
3950
- run: npm run gitdiff:ci
@@ -43,7 +54,7 @@ jobs:
4354
- uses: actions/checkout@v2
4455
- uses: actions/setup-node@v1
4556
with:
46-
node-version: 14
57+
node-version: 20
4758
registry-url: https://registry.npmjs.org/
4859
- run: npm i
4960
- run: npm run lint

benchmarks/main.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { Suite } from "bench-node";
2+
import * as nodeTools from "../src/mjs/index.js";
3+
import * as browserTools from "../src/mjs/browser.js";
4+
5+
const DUMMY_BUFFER = Uint8Array.from([
6+
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde,
7+
0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
8+
0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe,
9+
0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
10+
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde,
11+
0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
12+
0xbe, 0xef,
13+
]);
14+
const DUMMY_HEX =
15+
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
16+
17+
// Note: only include benchmarks for functions with differing implementations for Node and Browser
18+
const BENCHMARKS = [
19+
[
20+
`fromHex`,
21+
(library) => () => {
22+
library.fromHex(DUMMY_HEX);
23+
},
24+
],
25+
[
26+
`toHex`,
27+
(library) => () => {
28+
library.toHex(DUMMY_BUFFER);
29+
},
30+
],
31+
];
32+
33+
const LIBRARIES = [
34+
["Node ", nodeTools],
35+
["Browser", browserTools],
36+
];
37+
38+
function setUpSuite(suite, platform, library, funcName, func) {
39+
suite.add(`${platform} ${funcName}`, func(library));
40+
}
41+
42+
async function main() {
43+
const suite = new Suite();
44+
for (const [funcName, func] of BENCHMARKS) {
45+
for (const [name, library] of LIBRARIES) {
46+
setUpSuite(suite, name, library, funcName, func);
47+
}
48+
}
49+
const results = await suite.run();
50+
51+
const nodeSlower = [];
52+
53+
for (let i = 0; i < results.length; i += 2) {
54+
const nodeResult = results[i];
55+
const browserResult = results[i + 1];
56+
if (nodeResult.opsSec < browserResult.opsSec) {
57+
nodeSlower.push(BENCHMARKS[i / 2][0]);
58+
}
59+
}
60+
if (nodeSlower.length > 0) {
61+
throw new Error(
62+
`\n*** Node was slower in the following:\n *** ${nodeSlower.join(
63+
"\n *** "
64+
)}`
65+
);
66+
}
67+
}
68+
69+
main().catch((err) => {
70+
console.error(err);
71+
process.exit(1);
72+
});

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
"types": "src/cjs/index.d.ts",
2424
"type": "module",
2525
"scripts": {
26-
"build": "npm run clean && npm run build-ts && npm run convert-cjs && rm -f ./src/cjs/browser.d.ts",
26+
"benchmarks": "node benchmarks/main.js",
27+
"build": "npm run clean && npm run build-ts && npm run convert-cjs",
2728
"build-ts": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json",
2829
"clean": "rm -rf ./src/* && rm -rf ./coverage && rm -f ./package-lock.json",
29-
"convert-cjs": "for f in ./src/cjs/*.js; do mv -- \"$f\" \"${f%.js}.cjs\"; done",
30+
"convert-cjs": "for f in ./src/cjs/*.js; do sed -i 's/\\.js\"/.cjs\"/g' \"$f\"; mv -- \"$f\" \"${f%.js}.cjs\"; done",
3031
"coverage": "npm run unit -- --coverage",
3132
"eslint": "eslint ts_src/*.ts",
3233
"format": "npm run eslint -- --fix",
@@ -50,6 +51,7 @@
5051
"@types/node": "16.11.1",
5152
"@typescript-eslint/eslint-plugin": "5.0.0",
5253
"@typescript-eslint/parser": "5.0.0",
54+
"bench-node": "0.0.1-beta.0",
5355
"eslint": "8.0.1",
5456
"eslint-config-prettier": "8.3.0",
5557
"eslint-plugin-prettier": "4.0.0",

src/cjs/browser.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export declare function toUtf8(bytes: Uint8Array): string;
2+
export declare function toHex(bytes: Uint8Array): string;
3+
export declare function fromHex(hexString: string): Uint8Array;
4+
export declare type CompareResult = -1 | 0 | 1;
5+
export declare function compare(v1: Uint8Array, v2: Uint8Array): CompareResult;

src/cjs/index.cjs

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
4-
function toUtf8(bytes) {
5-
return Buffer.from(bytes || []).toString();
6-
}
7-
exports.toUtf8 = toUtf8;
3+
exports.toUtf8 = exports.compare = exports.fromHex = exports.toHex = void 0;
84
function toHex(bytes) {
95
return Buffer.from(bytes || []).toString("hex");
106
}
@@ -13,7 +9,6 @@ function fromHex(hexString) {
139
return Uint8Array.from(Buffer.from(hexString || "", "hex"));
1410
}
1511
exports.fromHex = fromHex;
16-
function compare(v1, v2) {
17-
return Buffer.from(v1).compare(Buffer.from(v2));
18-
}
19-
exports.compare = compare;
12+
var browser_js_1 = require("./browser.cjs");
13+
Object.defineProperty(exports, "compare", { enumerable: true, get: function () { return browser_js_1.compare; } });
14+
Object.defineProperty(exports, "toUtf8", { enumerable: true, get: function () { return browser_js_1.toUtf8; } });

src/cjs/index.d.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
export declare function toUtf8(bytes: Uint8Array): string;
21
export declare function toHex(bytes: Uint8Array): string;
32
export declare function fromHex(hexString: string): Uint8Array;
4-
export declare type CompareResult = -1 | 0 | 1;
5-
export declare function compare(v1: Uint8Array, v2: Uint8Array): CompareResult;
3+
export { compare, CompareResult, toUtf8 } from './browser.js';

src/mjs/index.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
export function toUtf8(bytes) {
2-
return Buffer.from(bytes || []).toString();
3-
}
41
export function toHex(bytes) {
52
return Buffer.from(bytes || []).toString("hex");
63
}
74
export function fromHex(hexString) {
85
return Uint8Array.from(Buffer.from(hexString || "", "hex"));
96
}
10-
export function compare(v1, v2) {
11-
return Buffer.from(v1).compare(Buffer.from(v2));
12-
}
7+
export { compare, toUtf8 } from './browser.js';

ts_src/index.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
export function toUtf8(bytes: Uint8Array): string {
2-
return Buffer.from(bytes || []).toString();
3-
}
4-
51
export function toHex(bytes: Uint8Array): string {
62
return Buffer.from(bytes || []).toString("hex");
73
}
@@ -10,7 +6,4 @@ export function fromHex(hexString: string): Uint8Array {
106
return Uint8Array.from(Buffer.from(hexString || "", "hex"));
117
}
128

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

0 commit comments

Comments
 (0)