index.js 9.8 KB

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