{"record":{"id":"091fb65deabd60f8","repo":"denoland/deno","slug":"err-illegal-constructor","errorCode":"ERR_ILLEGAL_CONSTRUCTOR","errorMessage":"Illegal constructor","messagePattern":"Illegal constructor","errorType":"exception","errorClass":"ERR_ILLEGAL_CONSTRUCTOR","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/perf_hooks.js","lineNumber":325,"sourceCode":"function getHistogramCloneId(histogram) {\n  if (histogram[_cloneId] === undefined) {\n    histogram[_cloneId] = nextHistogramCloneId++;\n  }\n  MapPrototypeSet(\n    histogramCloneRegistry,\n    histogram[_cloneId],\n    histogram[_handle],\n  );\n  return histogram[_cloneId];\n}\n\n// Public, JS-side Histogram class - `instanceof` works against both\n// `monitorEventLoopDelay()` results and `createHistogram()` results.\nclass Histogram {\n  constructor(handle) {\n    // Intentionally not user-constructable.\n    if (handle === undefined) {\n      throw new ERR_ILLEGAL_CONSTRUCTOR();\n    }\n    this[_handle] = handle;\n  }\n\n  get count() {\n    return this[_handle].count;\n  }\n  get countBigInt() {\n    return this[_handle].countBigInt;\n  }\n  get min() {\n    if (this.max === 0) return EMPTY_HISTOGRAM_MIN;\n    return this[_handle].min;\n  }\n  get minBigInt() {\n    if (this.max === 0) return EMPTY_HISTOGRAM_MIN_BIGINT;\n    return BigInt(this[_handle].minBigInt);\n  }","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/perf_hooks.js#L307-L343","documentation":"The Histogram class exported from node:perf_hooks is intentionally not user-constructable: its constructor needs an internal handle and throws ERR_ILLEGAL_CONSTRUCTOR when handle === undefined (i.e. any external `new` call). Histogram instances are only meant to be obtained from monitorEventLoopDelay() or createHistogram().","triggerScenarios":"new perf_hooks.Histogram(); new (require(\"perf_hooks\")).Histogram(); subclassing with class MyH extends perf_hooks.Histogram { constructor() { super(); } } — the subclass path also hits the undefined-handle branch.","commonSituations":"Trying to fabricate a histogram for tests or seeding; adapting older blog samples referencing internal IntervalHistogram; TypeScript code instantiating the exported type directly.","solutions":["Get instances from factories: perf_hooks.createHistogram() or perf_hooks.monitorEventLoopDelay()","For tests, create a real histogram via createHistogram() and record() synthetic values into it","Replace subclassing with composition: wrap a real histogram instance instead of extending Histogram"],"exampleFix":"// before\nconst h = new perf_hooks.Histogram();\n\n// after\nconst { createHistogram } = require(\"node:perf_hooks\");\nconst h = createHistogram();","handlingStrategy":"validation","validationCode":"const { createHistogram } = require(\"node:perf_hooks\");\nconst h = createHistogram(); // factories only — never `new Histogram()`","typeGuard":"const RecordableHistogram = require(\"node:perf_hooks\").createHistogram().constructor;\nconst isHistogramInstance = (v) => v instanceof RecordableHistogram;","tryCatchPattern":"try {\n  const h = new perf_hooks.Histogram();\n} catch (err) {\n  if (err.code === \"ERR_ILLEGAL_CONSTRUCTOR\") {\n    h = perf_hooks.createHistogram();\n  } else throw err;\n}","preventionTips":["Treat exported perf_hooks classes as opaque — obtain instances only from createHistogram()/monitorEventLoopDelay()","Lint for `new perf_hooks.` in code review","In tests, build fixtures from real factory instances"],"tags":["perf-hooks","histogram","illegal-constructor","node-compat","deno"],"backgroundTag":"illegal-constructor","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}