Options
All
  • Public
  • Public/Protected
  • All
Menu

JavaScript Function Benchmark Utility

GitHub npm type definitions

A library to benchmark JavaScript functions with microsecond precision.

Docs | Github | NPM

Install

npm i function-benchmark-util -D

Usage

import { benchmark, benchmarkTimes } from 'function-benchmark-util';

function someFunction(param1, param2) {
    // Does something
};

const result1 = benchmark(someFunction, ['param1', 'param2']);

// Running multiple iterations
const result2 = benchmarkTimes(someFunction, 10, ['param1', 'param2']);
// See the results
console.table([result1, result2]);

BenchmarkResult object

All benchmark functions, including benchmark, benchmarkTimes, benchmarkWithThis,benchmarkTimesWithThis return a BenchmarkResult object.

Property type & Description
functionName string Name of the executed function
runCount number Execution count passed to benchmarkTimes and benchmarkTimesWithThis functions.
min number Minimum duration scored in microseconds
max number Maximum duration scored in microseconds
average number The average duration of all executions in microseconds
fnReturns any Latest returned function result during execution(s)

this context

Intentionally benchmark and benchmarkTimes functions set this context of the executed function to null to avoid ambiguity.

If you need to set this, use benchmarkWithThis and for multiple executions, use benchmarkTimesWithThis.

For more, see the documentation and examples.

License

MIT © Cengiz Can

David David npm bundle size

TODO: improve docs and examples.

Index

Functions

benchmark

  • Benchmark a function only once with its parameters and sets this context to null.

    Parameters

    • fn: any

      Function to benchmark

    • Optional params: any[]

      Parameters of function as an array

    Returns BenchmarkResult

benchmarkTimes

  • Benchmark a function multiple times with its parameters and sets this context to null.

    Parameters

    • fn: any

      Function to benchmark

    • times: number

      Times to execute function

    • params: any[]

      Parameters of function as an array

    Returns BenchmarkResult

benchmarkTimesWithThis

  • benchmarkTimesWithThis(fn: any, times: number, thisContext: object, params?: any[]): BenchmarkResult
  • Benchmark a function multiple times with its parameters and sets this context according to passed parameter.

    Parameters

    • fn: any

      Function to benchmark

    • times: number

      Times to execute function

    • thisContext: object

      this context for the function

    • Optional params: any[]

      Parameters of function as an array

    Returns BenchmarkResult

benchmarkWithThis

  • benchmarkWithThis(fn: any, thisContext: object, params?: any[]): BenchmarkResult
  • Benchmark a function only once with its parameters and sets this context according to passed parameter.

    Parameters

    • fn: any

      Function to benchmark

    • thisContext: object

      this context for the function

    • Optional params: any[]

      Parameters of function as an array

    Returns BenchmarkResult

convertTimeToMilliseconds

  • Converts microsecond time values to milliseconds in a given BenchmarkResult object. Does not mutates the input.

    Parameters

    • benchmarkResult: BenchmarkResult

      Benchmark result object

    • Default value precision: number = 2

      Precision digits. Default value is 2

    Returns BenchmarkResult

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc

Fork me on GitHub