-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
103 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters