fix!: remove deprecated statistics fields on TaskResult (#407) · tinylibs/tinybench@e5862b6
@@ -29,6 +29,10 @@ const hookNames = ['afterAll', 'beforeAll', 'beforeEach', 'afterEach'] as const
29293030const abortableStates = ['not-started', 'started'] as const
313132+const notStartedTaskResult: TaskResult = { state: 'not-started' }
33+const abortedTaskResult: TaskResult = { state: 'aborted' }
34+const startedTaskResult: TaskResult = { state: 'started' }
35+3236/**
3337 * A class that represents each benchmark task in Tinybench. It keeps track of the
3438 * results, name, the task function, the number times the task function has been executed, ...
@@ -46,15 +50,6 @@ export class Task extends EventTarget {
4650options?: RemoveEventListenerOptionsArgument
4751) => void
485249-/**
50- * The result object
51- */
52-result: Readonly<TaskResult & TaskResultRuntimeInfo> = {
53-runtime: 'unknown',
54-runtimeVersion: 'unknown',
55-state: 'not-started',
56-}
57-5853/**
5954 * The number of times the task function has been executed
6055 */
@@ -64,6 +59,14 @@ export class Task extends EventTarget {
6459return this.#name
6560}
666162+get result (): TaskResult & TaskResultRuntimeInfo {
63+return {
64+ ...this.#result,
65+runtime: this.#bench.runtime,
66+runtimeVersion: this.#bench.runtimeVersion,
67+}
68+}
69+6770/**
6871 * The task asynchronous status
6972 */
@@ -89,6 +92,11 @@ export class Task extends EventTarget {
8992 */
9093readonly #name: string
919495+/**
96+ * The result object
97+ */
98+ #result: TaskResult = notStartedTaskResult
99+92100/**
93101 * The task-level abort signal
94102 */
@@ -148,7 +156,7 @@ export class Task extends EventTarget {
148156if (emit) this.dispatchEvent(new BenchEvent('reset', this))
149157this.runs = 0
150158151-this.#setTaskResult({ state: this.#aborted ? 'aborted' : 'not-started' })
159+this.#result = this.#aborted ? abortedTaskResult : notStartedTaskResult
152160}
153161154162/**
@@ -157,12 +165,10 @@ export class Task extends EventTarget {
157165 * @internal
158166 */
159167async run (): Promise<Task> {
160-if (this.result.state !== 'not-started') {
168+if (this.#result.state !== 'not-started') {
161169return this
162170}
163-this.#setTaskResult({
164-state: 'started'
165-})
171+this.#result = { state: 'started' }
166172this.dispatchEvent(new BenchEvent('start', this))
167173await this.#bench.opts.setup(this, 'run')
168174const { error, samples: latencySamples } = (await this.#benchmark(
@@ -183,17 +189,15 @@ export class Task extends EventTarget {
183189 * @internal
184190 */
185191runSync (): this {
186-if (this.result.state !== 'not-started') {
192+if (this.#result.state !== 'not-started') {
187193return this
188194}
189195190196invariant(
191197this.#bench.concurrency === null,
192198'Cannot use `concurrency` option when using `runSync`'
193199)
194-this.#setTaskResult({
195-state: 'started'
196-})
200+this.#result = startedTaskResult
197201this.dispatchEvent(new BenchEvent('start', this))
198202199203const setupResult = this.#bench.opts.setup(this, 'run')
@@ -224,7 +228,7 @@ export class Task extends EventTarget {
224228 * @internal
225229 */
226230async warmup (): Promise<void> {
227-if (this.result.state !== 'not-started') {
231+if (this.#result.state !== 'not-started') {
228232return
229233}
230234this.dispatchEvent(new BenchEvent('warmup', this))
@@ -244,7 +248,7 @@ export class Task extends EventTarget {
244248 * @internal
245249 */
246250warmupSync (): void {
247-if (this.result.state !== 'not-started') {
251+if (this.#result.state !== 'not-started') {
248252return
249253}
250254@@ -464,11 +468,9 @@ export class Task extends EventTarget {
464468465469 #onAbort (): void {
466470if (
467-abortableStates.includes(this.result.state as typeof abortableStates[number])
471+abortableStates.includes(this.#result.state as typeof abortableStates[number])
468472) {
469-this.#setTaskResult({
470-state: 'aborted',
471-})
473+this.#result = abortedTaskResult
472474const ev = new BenchEvent('abort', this)
473475this.dispatchEvent(ev)
474476this.#bench.dispatchEvent(ev)
@@ -477,10 +479,9 @@ export class Task extends EventTarget {
477479478480 #postWarmup (error: Error | undefined): void {
479481if (error) {
480-this.#setTaskResult({
481- error,
482-state: 'errored',
483-})
482+/* eslint-disable perfectionist/sort-objects */
483+this.#result = { state: 'errored', error }
484+/* eslint-enable perfectionist/sort-objects */
484485const ev = new BenchEvent('error', this, error)
485486this.dispatchEvent(ev)
486487this.#bench.dispatchEvent(ev)
@@ -520,28 +521,27 @@ export class Task extends EventTarget {
520521sortSamples(throughputSamples)
521522const throughputStatistics = getStatisticsSorted(throughputSamples)
522523523-this.#setTaskResult({
524- period: totalTime / this.runs,
524+/* eslint-disable perfectionist/sort-objects */
525+this.#result = {
525526state: this.#aborted ? 'aborted-with-statistics' : 'completed',
526- totalTime,
527-// deprecated statistics included for backward compatibility
528- ...latencyStatistics,
529-hz: throughputStatistics.mean,
530527latency: latencyStatistics,
528+period: totalTime / this.runs,
531529throughput: throughputStatistics,
532-})
530+ totalTime,
531+}
532+/* eslint-enable perfectionist/sort-objects */
533533} else if (this.#aborted) {
534534// If aborted with no samples, still set the aborted flag
535-this.#setTaskResult({
536-state: 'aborted',
537-})
535+this.#result = abortedTaskResult
538536}
539537540538if (error) {
541-this.#setTaskResult({
542- error,
539+/* eslint-disable perfectionist/sort-objects */
540+this.#result = {
543541state: 'errored',
544-})
542+ error,
543+}
544+/* eslint-enable perfectionist/sort-objects */
545545const ev = new BenchEvent('error', this, error)
546546this.dispatchEvent(ev)
547547this.#bench.dispatchEvent(ev)
@@ -556,18 +556,6 @@ export class Task extends EventTarget {
556556// cycle and complete are equal in Task
557557this.dispatchEvent(new BenchEvent('complete', this))
558558}
559-560-/**
561- * set the result object values
562- * @param result - the task result object to merge with the current result object values
563- */
564- #setTaskResult (result: TaskResult): void {
565-this.result = Object.freeze({
566-runtime: this.#bench.runtime,
567-runtimeVersion: this.#bench.runtimeVersion,
568- ...result,
569-})
570-}
571559}
572560573561/**