{"record":{"id":"250a7657136d639c","repo":"vitest-dev/vitest","slug":"bench-compare-expects-every-argument-to-be-the","errorCode":null,"errorMessage":"`bench.compare()` expects every argument to be the return value of `bench` or `bench.from`.","messagePattern":"`bench\\.compare\\(\\)` expects every argument to be the return value of `bench` or `bench\\.from`\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/benchmark.ts","lineNumber":479,"sourceCode":"    const isOptions = lastArg != null && typeof lastArg === 'object' && !(kRegistration in lastArg)\n    const benchOptions = isOptions ? args.pop() as BenchRunOptions : undefined\n    const registrations = args as BenchRegistration<any>[]\n\n    // Mark every passed-in registration as consumed before validation so a\n    // throwing `bench.compare()` (wrong arity, wrong shape) doesn't also\n    // trigger the unrun-bench warning — the user's intent was to consume them.\n    for (const reg of registrations) {\n      if (reg != null && typeof reg === 'object' && kRegistration in reg) {\n        pending.delete(reg)\n      }\n    }\n\n    if (registrations.length < 2) {\n      throw new SyntaxError(`\\`bench.compare()\\` requires at least 2 benchmarks, received ${registrations.length} instead. ${registrations.length === 1 ? 'Consider calling `bench().run()`. ' : 'Define benchmarks by calling `bench()`. '}See https://vitest.dev/guide/benchmarking#comparing-benchmarks`)\n    }\n    for (const reg of registrations) {\n      if (reg == null || typeof reg !== 'object' || !(kRegistration in reg)) {\n        throw new SyntaxError('`bench.compare()` expects every argument to be the return value of `bench` or `bench.from`.')\n      }\n    }\n\n    const runnable: RunnableRegistration<any>[] = []\n    const fromEntries: FromRegistration<any>[] = []\n    for (const reg of registrations) {\n      if (isFromRegistration(reg)) {\n        fromEntries.push(reg)\n      }\n      else {\n        runnable.push(reg as RunnableRegistration<any>)\n      }\n    }\n\n    const taskMeta = new Map<string, TaskMeta>()\n    for (const reg of runnable) {\n      if (reg[kPerProject]) {\n        taskMeta.set(reg.name, { perProject: true })","sourceCodeStart":461,"sourceCodeEnd":497,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/benchmark.ts#L461-L497","documentation":"Every argument to `bench.compare(...)` (except an optional trailing options object) must be the return value of `bench()` or `bench.from()` — objects carrying the internal `kRegistration` symbol. This `SyntaxError` fires when any argument is null, a primitive, or a plain object that is not a registration.","triggerScenarios":"Calling `bench.compare(reg, 'not-a-reg')`, `bench.compare(reg, null)`, `bench.compare(reg, { name: 'x' })` (plain object without the symbol), or `bench.compare(reg, undefined)`. The loop at benchmark.ts:477-481 checks each arg for `kRegistration in reg`.","commonSituations":"Passing a benchmark function instead of its registration; passing the result of `.run()` (a promise/result) instead of the registration; conditional spreads that include `undefined`; passing a baseline data object instead of a `bench.from()` registration.","solutions":["Pass only values returned by `bench(...)` or `bench.from(...)`.","Filter out falsy values before spreading: `bench.compare(...regs.filter(Boolean))`.","If you intended to compare against a baseline, wrap it: `bench.from('name', () => baseline)` then pass the result."],"exampleFix":"// before\nbench.compare(regA, baselineData)\n\n// after\nconst regB = bench.from('baseline', () => baselineData)\nbench.compare(regA, regB)","handlingStrategy":"type-guard","validationCode":"if (!args.every(a => a != null && typeof a === 'object' && kRegistration in a)) throw new Error('all args must be bench()/bench.from() results')","typeGuard":"function isRegistration(v: unknown): v is BenchRegistration<any> { return v != null && typeof v === 'object' && typeof (v as any)?.[kRegistration] === 'boolean' }","tryCatchPattern":"null","preventionTips":["Only pass values directly returned by `bench()` or `bench.from()`.","Filter spreads with `.filter(Boolean)` to drop holes.","Wrap baseline data in `bench.from` before comparing."],"tags":["benchmark","compare","validation","argument-shape"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}