{"record":{"id":"35b8825c48d1e2e1","repo":"oven-sh/bun","slug":"bad-result","errorCode":null,"errorMessage":"bad result","messagePattern":"bad result","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"bench/snippets/date-now.mjs","lineNumber":8,"sourceCode":"// Copied from WebKit JSTests/microbenchmarks/date-now.js and date-now-elapsed.js.\n// Not a mitata benchmark: run directly with `bun date-now.mjs`.\n{\n  let sum = 0;\n  const t0 = performance.now();\n  for (let i = 0; i < 1e6; ++i) sum += Date.now();\n  const t1 = performance.now();\n  if (sum < 0) throw new Error(\"bad result\");\n  console.log(\"Date.now() x 1e6:\".padEnd(28), (t1 - t0).toFixed(2), \"ms\");\n}\n\n{\n  let sum = 0;\n  const start = Date.now();\n  const t0 = performance.now();\n  for (let i = 0; i < 1e6; ++i) {\n    const diff = Date.now() - start;\n    if (diff >= 0 && diff < 1e9) sum += diff;\n  }\n  const t1 = performance.now();\n  if (sum < 0) throw new Error(\"bad result\");\n  console.log(\"Date.now() elapsed x 1e6:\".padEnd(28), (t1 - t0).toFixed(2), \"ms\");\n}\n","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/bench/snippets/date-now.mjs#L1-L24","documentation":"Sanity check in the Date.now microbenchmark copied from WebKit's JSTests/microbenchmarks. It sums one million Date.now() return values; epoch milliseconds are around 1.7e12, so a positive sum of that magnitude is guaranteed for a working clock. `sum < 0` therefore means Date.now() itself returned invalid (negative or corrupted) values, not that the benchmark was misused.","triggerScenarios":"Calling Date.now() one million times in a tight loop and accumulating the results when the engine's date implementation returns garbage. The snippet is run directly with `bun date-now.mjs` (it is not a mitata benchmark).","commonSituations":"A system/VM clock set before 1970-01-01 UTC; broken RTC in a container or sandbox with time virtualization; a JSC Date regression in a custom Bun build.","solutions":["Verify the host clock: `date -u` should show a post-1970 date","Run the same loop under Node to separate a system clock problem from a Bun/JSC problem","If only Bun returns bad values, reduce to `for(...) sum += Date.now()` and report to oven-sh/bun with the output"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// cheap sanity check before trusting Date.now in this environment\nconst t = Date.now();\nif (!(t > 0) || !Number.isFinite(t)) throw new Error('Date.now returned an invalid value: ' + t);","typeGuard":"function isValidEpochMs(t) {\n  return typeof t === 'number' && Number.isFinite(t) && t > 0 && t < 4102444800000; // 1970..2100\n}","tryCatchPattern":null,"preventionTips":["Validate clock readings once at startup in clock-sensitive code instead of discovering garbage after a long run","Use performance.now() for elapsed-time measurement — it is monotonic and immune to wall-clock corruption","On VMs/containers, verify time sync (date -u, timedatectl) before running timing benchmarks"],"tags":["date","time","benchmark","assertion"],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}