0.2.4 • Published 20 days ago

extra-benchmark v0.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
20 days ago

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>
}
0.2.4

20 days ago

0.2.3

11 months ago

0.2.1

1 year ago

0.2.0

1 year ago

0.2.2

1 year ago

0.1.1

2 years ago

0.1.0

2 years ago