Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
chore: fix benchmark (#1243)
Browse files Browse the repository at this point in the history
  • Loading branch information
danez authored Nov 2, 2022
1 parent 53d6526 commit 4587763
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions benchmarks/helpers/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
/* eslint-disable max-statements */
import assert from 'assert'
import { performance, PerformanceObserver } from 'perf_hooks'
import { fileURLToPath } from 'url'

export const FIXTURES_DIR = fileURLToPath(new URL('../fixtures', import.meta.url))

const sleep = (ms) =>
new Promise((resolve) => {
setTimeout(resolve, ms)
})

export const timeFunction = async (func, runs = 1) => {
const finishedRuns = new Map()

performance.clearMarks()
performance.clearMeasures()

const observer = new PerformanceObserver((list) => {
const [entry] = list.getEntries()

Expand All @@ -14,17 +24,23 @@ export const timeFunction = async (func, runs = 1) => {

observer.observe({ entryTypes: ['measure'] })

await Promise.all(
Array.from({ length: runs }).map(async (_, index) => {
performance.mark(`run-${index}-start`)
for (let index = 0; index < runs; index++) {
performance.mark(`run-${index}-start`)

await func(index)

await func(index)
performance.measure(`run-${index}`, `run-${index}-start`)
}

performance.measure(`run-${index}`, `run-${index}-start`)
}),
)
// wait for PerformanceObserver to gather all data
await sleep(100)

const durations = [...finishedRuns.values()]

observer.disconnect()

assert(durations.length === runs, 'Not all runs produced timings')

const average = durations.reduce((acc, duration) => duration + acc, 0) / runs

return average
Expand Down

1 comment on commit 4587763

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱ Benchmark results

largeDepsEsbuild: 2.1s

largeDepsNft: 9.4s

largeDepsZisi: 14.5s

Please sign in to comment.