{"record":{"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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/integrations/chai/bench.ts#L10-L46","documentation":"Same .toBeFasterThan matcher, but this check fires when the actual side passed isBenchResult and the expected (second argument) did not. The expected value must also be a BenchResult with latency.mean so the matcher can compute the speed threshold.","triggerScenarios":"Calling expect(benchResult).toBeFasterThan(notABenchResult) where the second argument is a raw number, string, or object without the latency structure.","commonSituations":"Passing a threshold number as the second argument instead of another bench result; comparing against a stale or partial object; misunderstanding that both sides must be bench results.","solutions":["Pass another full BenchResult (from a different bench() task) as the second argument.","If you want a percentage threshold, use the options.delta parameter, not a raw number as the second arg.","Verify both arguments are objects with `latency.mean` numbers before calling the matcher."],"exampleFix":"// before — second arg is a raw number:\nexpect(result).toBeFasterThan(100)\n\n// after — second arg is a bench result, delta in options:\nexpect(result).toBeFasterThan(baseline, { delta: 0.1 })","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isBenchResult(v: unknown): v is { latency: { mean: number }; throughput: { mean: number } } {\n  return typeof v === 'object' && v !== null\n    && 'latency' in v && typeof (v as any).latency?.mean === 'number'\n}","tryCatchPattern":null,"preventionTips":["Pass a second bench result as the expected argument, not a raw number.","Use options.delta for percentage thresholds."],"tags":["bench","matcher","type-validation"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}