Package detail

extra-benchmark

BlackGlory26MIT0.2.4

Lightweight benchmarking library.

readme

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

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.2.4 (2024-04-19)

Bug Fixes

  • average memory increments (adf7641)

0.2.3 (2023-06-10)

Bug Fixes

0.2.2 (2022-11-28)

Bug Fixes

  • performance for Node.js v14 (cc65ecf)

0.2.1 (2022-11-27)

Bug Fixes

  • the behavior related to afterAll (8fd13aa)

0.2.0 (2022-11-27)

⚠ BREAKING CHANGES

    • Remove operationsPerNanosecond of IBenchmarkCaseResult
  • maximumElapsedTime, minimumElapsedTime, averageElapsedTime uints changed to milliseconds
  • Renamed warmUps to warms

  • remove BigInt values (93195cc)

  • rename warmUps to warms (4b64747)

0.1.1 (2022-09-10)

0.1.0 (2022-09-09)

Features