Skip to content

Commit

Permalink
build: add a benchmark command to compare versions
Browse files Browse the repository at this point in the history
  • Loading branch information
niieani committed Feb 3, 2020
1 parent 5964fe7 commit 16d4e1f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"build": "yarn run build:umd && yarn run build:esm && yarn run minify",
"clean": "rm -rf coverage yarn-debug.log",
"all": "yarn run lint && yarn run coverage && yarn run build && yarn run clean",
"semantic-release": "semantic-release"
"semantic-release": "semantic-release",
"benchmark": "yarn ts-node -O '{\"module\": \"commonjs\"}' -T tests/benchmark"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -79,7 +80,9 @@
"eslint-plugin-jest": "^23.6.0",
"husky": "^4.2.1",
"jest": "^25.1.0",
"nodemark": "^0.3.0",
"prettier": "^1.19.1",
"require-from-web": "^1.1.1",
"semantic-release": "^17.0.2",
"terser": "^4.6.3",
"ts-node": "^8.6.2",
Expand Down
37 changes: 37 additions & 0 deletions tests/benchmark.ts
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()
79 changes: 79 additions & 0 deletions tests/types.d.ts
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
}

0 comments on commit 16d4e1f

Please sign in to comment.