{"id":"4b783175b853f963","repo":"vitest-dev/vitest","slug":"matcherhint-tobefasterthan-expects-the-expe","errorCode":null,"errorMessage":"${matcherHint('.toBeFasterThan')} expects the expected value to be a benchmark result.","messagePattern":"(.+?) expects the expected value to be a benchmark result\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/integrations/chai/bench.ts","lineNumber":28,"sourceCode":"  )\n}\n\nfunction formatOps(ops: number): string {\n  return ops.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })\n}\n\nexport const benchMatchers: MatchersObject = {\n  toBeFasterThan(actual: unknown, expected: unknown, options?: { delta?: number }) {\n    const { matcherHint, RECEIVED_COLOR, EXPECTED_COLOR } = this.utils\n    const delta = options?.delta ?? 0\n\n    if (!isBenchResult(actual)) {\n      throw new TypeError(\n        `${matcherHint('.toBeFasterThan')} expects the actual value to be a benchmark result.`,\n      )\n    }\n    if (!isBenchResult(expected)) {\n      throw new TypeError(\n        `${matcherHint('.toBeFasterThan')} expects the expected value to be a benchmark result.`,\n      )\n    }\n\n    const threshold = expected.latency.mean * (1 - delta)\n    const pass = actual.latency.mean < threshold\n\n    return {\n      pass,\n      message: () => {\n        const relation = ((actual.latency.mean - expected.latency.mean) / expected.latency.mean * 100).toFixed(2)\n        return pass\n          ? `${matcherHint('.not.toBeFasterThan')}\\n\\nExpected to not be faster, but was ${Math.abs(Number(relation))}% faster.\\n\\n`\n          + `Received: ${RECEIVED_COLOR(formatOps(actual.throughput.mean))} ops/sec\\n`\n          + `Expected: ${EXPECTED_COLOR(formatOps(expected.throughput.mean))} ops/sec\\n`\n          : `${matcherHint('.toBeFasterThan')}\\n\\nExpected to be faster${delta > 0 ? ` by at least ${(delta * 100).toFixed(0)}%` : ''}, but was ${Number(relation) > 0 ? `${relation}% slower` : `only ${Math.abs(Number(relation))}% faster`}.\\n\\n`\n            + `Received: ${RECEIVED_COLOR(formatOps(actual.throughput.mean))} ops/sec\\n`\n            + `Expected: ${EXPECTED_COLOR(formatOps(expected.throughput.mean))} ops/sec\\n`","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/integrations/chai/bench.ts#L10-L46","documentation":"Companion to error 177, thrown by toBeFasterThan when the EXPECTED (second) argument is not a BenchResult. The matcher needs both operands to expose latency.mean and throughput.mean so it can compute the delta threshold and the ops/sec display. Passing a number/string/object-without-stats as the baseline triggers this guard right after the actual check.","triggerScenarios":"expect(result).toBeFasterThan(100); passing a numeric threshold instead of a baseline BenchResult; using a plain object as the baseline; passing the result of a non-bench measurement.","commonSituations":"Assuming toBeFasterThan takes a percentage/millisecond delta instead of a baseline result; mixing up toBeFasterThan with toBeLessThan; refactoring that replaced the baseline bench result with a number.","solutions":["Pass a real BenchResult as the second argument (one obtained from another bench() task).","Use the options.delta argument ({ delta: 0.1 }) to express a percentage threshold instead of a numeric baseline.","Build the baseline by running bench() against the reference implementation and storing the result.","Switch to toBeLessThan if you want a numeric comparison."],"exampleFix":"// before\nexpect(result).toBeFasterThan(100)\n\n// after\nexpect(result).toBeFasterThan(baselineResult, { delta: 0.1 })","handlingStrategy":"type-guard","validationCode":"function isBenchResult(value: unknown): value is { latency: { mean: number }; throughput: { mean: number } } {\n  return typeof value === 'object' && value !== null\n    && 'latency' in value && typeof (value as any).latency?.mean === 'number'\n}\nif (!isBenchResult(baseline)) {\n  throw new TypeError('baseline must be a BenchResult; use { delta } for thresholds')\n}","typeGuard":"function isBenchResult(value: unknown): value is { latency: { mean: number }; throughput: { mean: number } } {\n  return typeof value === 'object' && value !== null\n    && 'latency' in value && typeof (value as any).latency?.mean === 'number'\n}","tryCatchPattern":null,"preventionTips":["Pass a real BenchResult as the expected argument, not a number.","Use the options.delta field for percentage thresholds instead of a numeric baseline.","Generate baselines through bench() runs and persist them for deterministic comparisons."],"tags":["benchmark","matcher","type-mismatch"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}