@mudssky/jsutils
    Preparing search index...

    Class PerformanceMonitor

    性能检测器类

    提供测试JavaScript代码片段或方法执行性能的功能,支持多次迭代测试、 内存使用监控、预热执行等高级特性。

    const perf = new PerformanceMonitor()

    // 测试函数性能
    const result = await perf.measureFunction(() => {
    return Array.from({ length: 1000 }, (_, i) => i * 2)
    })
    console.log(`执行时间: ${result.duration}ms`)

    // 测试异步函数性能
    const asyncResult = await perf.measureFunction(async () => {
    await new Promise(resolve => setTimeout(resolve, 100))
    return 'done'
    })

    // 多次迭代测试
    const iterResult = await perf.measureFunction(
    () => Math.random(),
    { iterations: 1000, warmupIterations: 100 }
    )

    // 比较两个函数的性能
    const comparison = await perf.compare(
    () => [1, 2, 3].map(x => x * 2),
    () => [1, 2, 3].forEach((x, i, arr) => arr[i] = x * 2),
    { iterations: 10000 }
    )

    1.0.0

    Index

    Constructors

    Methods

    • 批量测试多个函数的性能

      Type Parameters

      • T

      Parameters

      Returns Promise<PerformanceResult[]>

      • 所有函数的性能结果数组
      const perf = new PerformanceMonitor()

      const results = await perf.benchmark([
      () => [1, 2, 3].map(x => x * 2),
      () => [1, 2, 3].forEach((x, i, arr) => arr[i] = x * 2),
      () => {
      const result = []
      for (const x of [1, 2, 3]) {
      result.push(x * 2)
      }
      return result
      }
      ], { iterations: 10000 })

      results.forEach((result, index) => {
      console.log(`函数${index + 1}: ${result.duration}ms`)
      })
    • 比较两个函数的性能

      Type Parameters

      • T1
      • T2

      Parameters

      Returns Promise<
          {
              faster: "fn1"
              | "fn2";
              fn1: PerformanceResult;
              fn2: PerformanceResult;
              ratio: number;
          },
      >

      • 包含两个函数性能结果的对象
      const perf = new PerformanceMonitor()

      const comparison = await perf.compare(
      () => [1, 2, 3].map(x => x * 2),
      () => {
      const result = []
      for (const x of [1, 2, 3]) {
      result.push(x * 2)
      }
      return result
      },
      { iterations: 10000 }
      )

      console.log(`函数1: ${comparison.fn1.duration}ms`)
      console.log(`函数2: ${comparison.fn2.duration}ms`)
      console.log(`性能差异: ${comparison.ratio.toFixed(2)}x`)
    • 创建性能报告

      Parameters

      • results: PerformanceResult[]

        性能测试结果数组

      • Optionallabels: string[]

        可选的标签数组

      Returns string

      • 格式化的性能报告
      const perf = new PerformanceMonitor()
      const results = await perf.benchmark([fn1, fn2, fn3])
      const report = perf.createReport(results, ['Map', 'ForEach', 'For Loop'])
      console.log(report)
    • 格式化性能结果为可读字符串

      Parameters

      Returns string

      • 格式化的字符串
      const perf = new PerformanceMonitor()
      const result = await perf.measureFunction(() => someFunction())
      console.log(perf.formatResult(result))
    • 测试函数执行性能

      Type Parameters

      • T

      Parameters

      Returns Promise<PerformanceResult>

      • 性能测试结果
      const perf = new PerformanceMonitor()

      // 基本测试
      const result = await perf.measureFunction(() => {
      return Array.from({ length: 1000 }, (_, i) => i * 2)
      })

      // 带选项的测试
      const result2 = await perf.measureFunction(
      () => someExpensiveOperation(),
      {
      iterations: 100,
      warmupIterations: 10,
      collectMemory: true,
      forceGC: true
      }
      )