index.cjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. "use strict";
  2. var w = Object.defineProperty;
  3. var P = Object.getOwnPropertyDescriptor;
  4. var q = Object.getOwnPropertyNames;
  5. var V = Object.prototype.hasOwnProperty;
  6. var C = (s, r, t) => r in s ? w(s, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[r] = t;
  7. var $ = (s, r) => {
  8. for (var t in r)
  9. w(s, t, { get: r[t], enumerable: !0 });
  10. }, D = (s, r, t, e) => {
  11. if (r && typeof r == "object" || typeof r == "function")
  12. for (let n of q(r))
  13. !V.call(s, n) && n !== t && w(s, n, { get: () => r[n], enumerable: !(e = P(r, n)) || e.enumerable });
  14. return s;
  15. };
  16. var G = (s) => D(w({}, "__esModule", { value: !0 }), s);
  17. var i = (s, r, t) => (C(s, typeof r != "symbol" ? r + "" : r, t), t);
  18. // src/index.ts
  19. var Z = {};
  20. $(Z, {
  21. Bench: () => m,
  22. Task: () => p,
  23. default: () => Y,
  24. hrtimeNow: () => R,
  25. now: () => E
  26. });
  27. module.exports = G(Z);
  28. // src/event.ts
  29. function a(s, r = null) {
  30. let t = new Event(s);
  31. return Object.defineProperty(t, "task", {
  32. value: r,
  33. enumerable: !0,
  34. writable: !1,
  35. configurable: !1
  36. }), t;
  37. }
  38. // src/constants.ts
  39. var J = {
  40. 1: 12.71,
  41. 2: 4.303,
  42. 3: 3.182,
  43. 4: 2.776,
  44. 5: 2.571,
  45. 6: 2.447,
  46. 7: 2.365,
  47. 8: 2.306,
  48. 9: 2.262,
  49. 10: 2.228,
  50. 11: 2.201,
  51. 12: 2.179,
  52. 13: 2.16,
  53. 14: 2.145,
  54. 15: 2.131,
  55. 16: 2.12,
  56. 17: 2.11,
  57. 18: 2.101,
  58. 19: 2.093,
  59. 20: 2.086,
  60. 21: 2.08,
  61. 22: 2.074,
  62. 23: 2.069,
  63. 24: 2.064,
  64. 25: 2.06,
  65. 26: 2.056,
  66. 27: 2.052,
  67. 28: 2.048,
  68. 29: 2.045,
  69. 30: 2.042,
  70. 31: 2.0399,
  71. 32: 2.0378,
  72. 33: 2.0357,
  73. 34: 2.0336,
  74. 35: 2.0315,
  75. 36: 2.0294,
  76. 37: 2.0273,
  77. 38: 2.0252,
  78. 39: 2.0231,
  79. 40: 2.021,
  80. 41: 2.0198,
  81. 42: 2.0186,
  82. 43: 2.0174,
  83. 44: 2.0162,
  84. 45: 2.015,
  85. 46: 2.0138,
  86. 47: 2.0126,
  87. 48: 2.0114,
  88. 49: 2.0102,
  89. 50: 2.009,
  90. 51: 2.0081,
  91. 52: 2.0072,
  92. 53: 2.0063,
  93. 54: 2.0054,
  94. 55: 2.0045,
  95. 56: 2.0036,
  96. 57: 2.0027,
  97. 58: 2.0018,
  98. 59: 2.0009,
  99. 60: 2,
  100. 61: 1.9995,
  101. 62: 1.999,
  102. 63: 1.9985,
  103. 64: 1.998,
  104. 65: 1.9975,
  105. 66: 1.997,
  106. 67: 1.9965,
  107. 68: 1.996,
  108. 69: 1.9955,
  109. 70: 1.995,
  110. 71: 1.9945,
  111. 72: 1.994,
  112. 73: 1.9935,
  113. 74: 1.993,
  114. 75: 1.9925,
  115. 76: 1.992,
  116. 77: 1.9915,
  117. 78: 1.991,
  118. 79: 1.9905,
  119. 80: 1.99,
  120. 81: 1.9897,
  121. 82: 1.9894,
  122. 83: 1.9891,
  123. 84: 1.9888,
  124. 85: 1.9885,
  125. 86: 1.9882,
  126. 87: 1.9879,
  127. 88: 1.9876,
  128. 89: 1.9873,
  129. 90: 1.987,
  130. 91: 1.9867,
  131. 92: 1.9864,
  132. 93: 1.9861,
  133. 94: 1.9858,
  134. 95: 1.9855,
  135. 96: 1.9852,
  136. 97: 1.9849,
  137. 98: 1.9846,
  138. 99: 1.9843,
  139. 100: 1.984,
  140. 101: 1.9838,
  141. 102: 1.9836,
  142. 103: 1.9834,
  143. 104: 1.9832,
  144. 105: 1.983,
  145. 106: 1.9828,
  146. 107: 1.9826,
  147. 108: 1.9824,
  148. 109: 1.9822,
  149. 110: 1.982,
  150. 111: 1.9818,
  151. 112: 1.9816,
  152. 113: 1.9814,
  153. 114: 1.9812,
  154. 115: 1.9819,
  155. 116: 1.9808,
  156. 117: 1.9806,
  157. 118: 1.9804,
  158. 119: 1.9802,
  159. 120: 1.98,
  160. infinity: 1.96
  161. }, g = J;
  162. // src/utils.ts
  163. var Q = (s) => s / 1e6, R = () => Q(Number(process.hrtime.bigint())), E = () => performance.now();
  164. function U(s) {
  165. return s !== null && typeof s == "object" && typeof s.then == "function";
  166. }
  167. var O = (s) => s.reduce((r, t) => r + t, 0) / s.length || 0, F = (s, r) => s.reduce((e, n) => e + (n - r) ** 2, 0) / (s.length - 1) || 0, W = (async () => {
  168. }).constructor, X = (s) => s.constructor === W, k = async (s) => {
  169. if (X(s.fn))
  170. return !0;
  171. try {
  172. if (s.opts.beforeEach != null)
  173. try {
  174. await s.opts.beforeEach.call(s);
  175. } catch (e) {
  176. }
  177. let r = s.fn(), t = U(r);
  178. if (t)
  179. try {
  180. await r;
  181. } catch (e) {
  182. }
  183. if (s.opts.afterEach != null)
  184. try {
  185. await s.opts.afterEach.call(s);
  186. } catch (e) {
  187. }
  188. return t;
  189. } catch (r) {
  190. return !1;
  191. }
  192. };
  193. // src/task.ts
  194. var p = class extends EventTarget {
  195. constructor(t, e, n, h = {}) {
  196. super();
  197. i(this, "bench");
  198. i(this, "name");
  199. i(this, "fn");
  200. i(this, "runs", 0);
  201. i(this, "result");
  202. i(this, "opts");
  203. this.bench = t, this.name = e, this.fn = n, this.opts = h;
  204. }
  205. async run() {
  206. var h, c, f, d;
  207. this.dispatchEvent(a("start", this));
  208. let t = 0, e = [];
  209. if (await this.bench.setup(this, "run"), this.opts.beforeAll != null)
  210. try {
  211. await this.opts.beforeAll.call(this);
  212. } catch (o) {
  213. this.setResult({ error: o });
  214. }
  215. let n = await k(this);
  216. try {
  217. for (; (t < this.bench.time || this.runs < this.bench.iterations) && !((h = this.bench.signal) != null && h.aborted); ) {
  218. this.opts.beforeEach != null && await this.opts.beforeEach.call(this);
  219. let o = 0;
  220. if (n) {
  221. let l = this.bench.now();
  222. await this.fn.call(this), o = this.bench.now() - l;
  223. } else {
  224. let l = this.bench.now();
  225. this.fn.call(this), o = this.bench.now() - l;
  226. }
  227. e.push(o), this.runs += 1, t += o, this.opts.afterEach != null && await this.opts.afterEach.call(this);
  228. }
  229. } catch (o) {
  230. if (this.setResult({ error: o }), this.bench.throws)
  231. throw o;
  232. }
  233. if (this.opts.afterAll != null)
  234. try {
  235. await this.opts.afterAll.call(this);
  236. } catch (o) {
  237. this.setResult({ error: o });
  238. }
  239. if (await this.bench.teardown(this, "run"), !((c = this.result) != null && c.error)) {
  240. e.sort((j, z) => j - z);
  241. let o = t / this.runs, l = 1e3 / o, u = e.length, b = u - 1, B = e[0], K = e[b], T = O(e), y = F(e, T), A = Math.sqrt(y), x = A / Math.sqrt(u), L = g[String(Math.round(b) || 1)] || g.infinity, M = x * L, N = M / T * 100, _ = e[Math.ceil(u * 0.75) - 1], S = e[Math.ceil(u * 0.99) - 1], I = e[Math.ceil(u * 0.995) - 1], H = e[Math.ceil(u * 0.999) - 1];
  242. if ((f = this.bench.signal) != null && f.aborted)
  243. return this;
  244. this.setResult({
  245. totalTime: t,
  246. min: B,
  247. max: K,
  248. hz: l,
  249. period: o,
  250. samples: e,
  251. mean: T,
  252. variance: y,
  253. sd: A,
  254. sem: x,
  255. df: b,
  256. critical: L,
  257. moe: M,
  258. rme: N,
  259. p75: _,
  260. p99: S,
  261. p995: I,
  262. p999: H
  263. });
  264. }
  265. return (d = this.result) != null && d.error && (this.dispatchEvent(a("error", this)), this.bench.dispatchEvent(a("error", this))), this.dispatchEvent(a("cycle", this)), this.bench.dispatchEvent(a("cycle", this)), this.dispatchEvent(a("complete", this)), this;
  266. }
  267. async warmup() {
  268. var h;
  269. this.dispatchEvent(a("warmup", this));
  270. let t = this.bench.now(), e = 0;
  271. if (await this.bench.setup(this, "warmup"), this.opts.beforeAll != null)
  272. try {
  273. await this.opts.beforeAll.call(this);
  274. } catch (c) {
  275. this.setResult({ error: c });
  276. }
  277. let n = await k(this);
  278. for (; (e < this.bench.warmupTime || this.runs < this.bench.warmupIterations) && !((h = this.bench.signal) != null && h.aborted); ) {
  279. if (this.opts.beforeEach != null)
  280. try {
  281. await this.opts.beforeEach.call(this);
  282. } catch (c) {
  283. this.setResult({ error: c });
  284. }
  285. try {
  286. n ? await this.fn.call(this) : this.fn.call(this);
  287. } catch (c) {
  288. if (this.bench.throws)
  289. throw c;
  290. }
  291. if (this.runs += 1, e = this.bench.now() - t, this.opts.afterEach != null)
  292. try {
  293. await this.opts.afterEach.call(this);
  294. } catch (c) {
  295. this.setResult({ error: c });
  296. }
  297. }
  298. if (this.opts.afterAll != null)
  299. try {
  300. await this.opts.afterAll.call(this);
  301. } catch (c) {
  302. this.setResult({ error: c });
  303. }
  304. this.bench.teardown(this, "warmup"), this.runs = 0;
  305. }
  306. addEventListener(t, e, n) {
  307. super.addEventListener(t, e, n);
  308. }
  309. removeEventListener(t, e, n) {
  310. super.removeEventListener(t, e, n);
  311. }
  312. setResult(t) {
  313. this.result = { ...this.result, ...t }, Object.freeze(this.reset);
  314. }
  315. reset() {
  316. this.dispatchEvent(a("reset", this)), this.runs = 0, this.result = void 0;
  317. }
  318. };
  319. // src/bench.ts
  320. var m = class extends EventTarget {
  321. constructor(t = {}) {
  322. var e, n, h, c, f, d, o, l;
  323. super();
  324. i(this, "_tasks", /* @__PURE__ */ new Map());
  325. i(this, "_todos", /* @__PURE__ */ new Map());
  326. i(this, "signal");
  327. i(this, "throws");
  328. i(this, "warmupTime", 100);
  329. i(this, "warmupIterations", 5);
  330. i(this, "time", 500);
  331. i(this, "iterations", 10);
  332. i(this, "now", E);
  333. i(this, "setup");
  334. i(this, "teardown");
  335. this.now = (e = t.now) != null ? e : this.now, this.warmupTime = (n = t.warmupTime) != null ? n : this.warmupTime, this.warmupIterations = (h = t.warmupIterations) != null ? h : this.warmupIterations, this.time = (c = t.time) != null ? c : this.time, this.iterations = (f = t.iterations) != null ? f : this.iterations, this.signal = t.signal, this.throws = (d = t.throws) != null ? d : !1, this.setup = (o = t.setup) != null ? o : () => {
  336. }, this.teardown = (l = t.teardown) != null ? l : () => {
  337. }, this.signal && this.signal.addEventListener(
  338. "abort",
  339. () => {
  340. this.dispatchEvent(a("abort"));
  341. },
  342. { once: !0 }
  343. );
  344. }
  345. async run() {
  346. var e;
  347. this.dispatchEvent(a("start"));
  348. let t = [];
  349. for (let n of [...this._tasks.values()])
  350. (e = this.signal) != null && e.aborted ? t.push(n) : t.push(await n.run());
  351. return this.dispatchEvent(a("complete")), t;
  352. }
  353. async warmup() {
  354. this.dispatchEvent(a("warmup"));
  355. for (let [, t] of this._tasks)
  356. await t.warmup();
  357. }
  358. reset() {
  359. this.dispatchEvent(a("reset")), this._tasks.forEach((t) => {
  360. t.reset();
  361. });
  362. }
  363. add(t, e, n = {}) {
  364. let h = new p(this, t, e, n);
  365. return this._tasks.set(t, h), this.dispatchEvent(a("add", h)), this;
  366. }
  367. todo(t, e = () => {
  368. }, n = {}) {
  369. let h = new p(this, t, e, n);
  370. return this._todos.set(t, h), this.dispatchEvent(a("todo", h)), this;
  371. }
  372. remove(t) {
  373. let e = this.getTask(t);
  374. return e && (this.dispatchEvent(a("remove", e)), this._tasks.delete(t)), this;
  375. }
  376. addEventListener(t, e, n) {
  377. super.addEventListener(t, e, n);
  378. }
  379. removeEventListener(t, e, n) {
  380. super.removeEventListener(t, e, n);
  381. }
  382. table() {
  383. return this.tasks.map(({ name: t, result: e }) => e ? {
  384. "Task Name": t,
  385. "ops/sec": e.error ? "NaN" : parseInt(e.hz.toString(), 10).toLocaleString(),
  386. "Average Time (ns)": e.error ? "NaN" : e.mean * 1e3 * 1e3,
  387. Margin: e.error ? "NaN" : `\xB1${e.rme.toFixed(2)}%`,
  388. Samples: e.error ? "NaN" : e.samples.length
  389. } : null);
  390. }
  391. get results() {
  392. return [...this._tasks.values()].map((t) => t.result);
  393. }
  394. get tasks() {
  395. return [...this._tasks.values()];
  396. }
  397. get todos() {
  398. return [...this._todos.values()];
  399. }
  400. getTask(t) {
  401. return this._tasks.get(t);
  402. }
  403. };
  404. // src/index.ts
  405. var Y = m;
  406. // Annotate the CommonJS export names for ESM import in node:
  407. 0 && (module.exports = {
  408. Bench,
  409. Task,
  410. hrtimeNow,
  411. now
  412. });