0.2.4 • Published 8 months ago
extra-benchmark v0.2.4
extra-benchmark
Lightweight benchmarking library.
Install
npm install --save extra-benchmark
# or
yarn add extra-benchmark
Usage
import { Benchmark } from 'extra-benchmark'
const benchmark = new Benchmark('Swap')
benchmark.addCase('Bit-based computing', () => {
let a = 0
let b = 1
return () => {
a = a ^ b
b = a ^ b
a = a ^ b
}
})
benchmark.addCase('Use a temporary variable', () => {
let a = 0
let b = 1
return () => {
const temp = a
a = b
b = temp
}
})
console.log(benchmark.name)
for await (const result of benchmark.run()) {
console.log(result)
}
API
interface IBenchmarkOptions {
/* The number of times to warm up the benchmark test */
warms?: number
/* The number of times to run the benchmark test */
runs?: number
}
interface IBenchmarkCaseResult {
name: string
warms: number
runs: number
operationsPerSecond: number
operationsPerMillisecond: number
/* Milliseconds */
maxiumElapsedTime: bigint
minimumElapsedTime: bigint
averageElapsedTime: bigint
/* Bytes */
maximumMemoryIncrements: number
minimumMemoryIncrements: number
averageMemoryIncrements: number
}
Benchmark
class Benchmark {
readonly name: string
constructor(
name: string
, {
warms: 100
, runs: 100
}: IBenchmarkOptions = {}
)
addCase(
name: string
, fn: () => Awaitable<
// iterate(): afterEach
| (() => Awaitable<(() => Awaitable<void>) | Falsy>)
| {
// iterate(): afterEach
iterate: () => Awaitable<(() => Awaitable<void>) | Falsy>
beforeEach?: () => Awaitable<void>
afterAll?: () => Awaitable<void>
}
>
, options?: IBenchmarkResult
): void
run(): AsyncIterable<IBenchmarkCaseResult>
}