-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: add a benchmark command to compare versions
- Loading branch information
Showing
3 changed files
with
120 additions
and
1 deletion.
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,37 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
/* eslint-disable @typescript-eslint/camelcase */ | ||
/* eslint-disable no-console */ | ||
import benchmark from 'nodemark' | ||
import Hashids from '../lib/hashids' | ||
import requireFromWeb from 'require-from-web' | ||
|
||
type HashidsType = typeof import('../lib/hashids').default | ||
|
||
const benchmarkVersion = (Hashids: HashidsType, version: string) => { | ||
const hashids = new Hashids() | ||
const encoded = '5KoLLVL49RLhYkppOplM6piwWNNANny8N' | ||
const decoded = [9007199254740991, 9007199254740991, 9007199254740991] | ||
|
||
const decoding = benchmark(() => hashids.decode(encoded)) | ||
const encoding = benchmark(() => hashids.encode(decoded)) | ||
|
||
console.log(version, '\tdecoding\t', decoding) | ||
console.log(version, '\tencoding\t', encoding) | ||
} | ||
|
||
async function run() { | ||
const {default: Hashids_v1_2_2} = await requireFromWeb<{ | ||
default: HashidsType | ||
}>('https://unpkg.com/hashids@1.2.2/dist/index.js') | ||
const {default: Hashids_v2_1_0} = await requireFromWeb<{ | ||
default: HashidsType | ||
}>('https://unpkg.com/hashids@2.1.0/dist/hashids.js') | ||
const Hashids_transpiled = require('../cjs') | ||
|
||
benchmarkVersion(Hashids_v1_2_2, '1.2.2') | ||
benchmarkVersion(Hashids_v2_1_0, '2.1.0') | ||
benchmarkVersion(Hashids_transpiled, 'transpiled') | ||
benchmarkVersion(Hashids, 'node') | ||
} | ||
|
||
void run() |
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,79 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
declare module 'nodemark' { | ||
class BenchmarkResult { | ||
/** the average measured time in nanoseconds */ | ||
mean: number | ||
/** the margin of error as a ratio of the mean */ | ||
error: number | ||
/** the fastest measured time in nanoseconds */ | ||
max: number | ||
/** the slowest measured time in nanoseconds */ | ||
min: number | ||
/** the number of times the subject was invoked and measured */ | ||
count: number | ||
|
||
/** | ||
* Returns this.mean, rounded to the nearest whole number | ||
* or the number of decimal places specified by `precision` | ||
*/ | ||
nanoseconds(precision?: number): number | ||
/** | ||
* Returns this.mean, rounded to the nearest whole number | ||
* or the number of decimal places specified by `precision` | ||
*/ | ||
microseconds(precision?: number): number | ||
/** | ||
* Returns this.mean, rounded to the nearest whole number | ||
* or the number of decimal places specified by `precision` | ||
*/ | ||
milliseconds(precision?: number): number | ||
/** | ||
* Returns this.mean, rounded to the nearest whole number | ||
* or the number of decimal places specified by `precision` | ||
*/ | ||
seconds(precision?: number): number | ||
/** | ||
* Returns the average number of executions per second, | ||
* rounded to the nearest whole number or the number of decimal places specified by precision. | ||
*/ | ||
hz(precision?: number): number | ||
/** | ||
* Returns the standard deviation per second, | ||
* rounded to the nearest whole number or the number of decimal places specified by precision. | ||
*/ | ||
sd(precision?: number): number | ||
|
||
/** | ||
* Returns a nicely formatted string describing the result of the benchmark. | ||
* By default, the "hz" format is used, which displays ops/sec, | ||
* but you can optionally specify "nanoseconds", "microseconds", "milliseconds", | ||
* or "seconds" to change the displayed information. | ||
*/ | ||
toString( | ||
format?: | ||
| 'hz' | ||
| 'nanoseconds' | ||
| 'microseconds' | ||
| 'milliseconds' | ||
| 'seconds', | ||
): string | ||
} | ||
const benchmark: { | ||
( | ||
subject: () => any, | ||
setup?: () => any, | ||
durationMillis?: number, | ||
): BenchmarkResult | ||
( | ||
subject: (callback: (...args: Array<unknown>) => void) => any, | ||
setup?: () => any, | ||
durationMillis?: number, | ||
): Promise<BenchmarkResult> | ||
} | ||
export = benchmark | ||
} | ||
|
||
declare module 'require-from-web' { | ||
function requireFromWeb<T>(url: string): Promise<T> | ||
export = requireFromWeb | ||
} |