@guillaume-docquier/tools-ts 1.3.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ type BasicLogger = {
2
+ info: (message: string, context?: Record<string, unknown>) => void;
3
+ };
4
+ export declare const Measure: {
5
+ executionTime: typeof executionTime;
6
+ /**
7
+ * Prints the current memory usage using {@link https://nodejs.org/api/process.html#processmemoryusage process.memoryUsage()}.
8
+ */
9
+ memoryUsage(logger?: BasicLogger): void;
10
+ };
11
+ /**
12
+ * Measures the execution time of the given synchronous action and logs it.
13
+ * The result of the action is returned.
14
+ */
15
+ declare function executionTime<T>(label: string, action: () => Promise<T>, logger?: BasicLogger): Promise<T>;
16
+ /**
17
+ * Measures the execution time of the given asynchronous action and logs it.
18
+ * The result of the action is returned.
19
+ */
20
+ declare function executionTime<T>(label: string, action: () => T, logger?: BasicLogger): T;
21
+ export {};
@@ -0,0 +1,53 @@
1
+ export const Measure = {
2
+ // You can't overload while defining object properties, which is why the function implementation is not inlined
3
+ executionTime,
4
+ /**
5
+ * Prints the current memory usage using {@link https://nodejs.org/api/process.html#processmemoryusage process.memoryUsage()}.
6
+ */
7
+ memoryUsage(logger = console) {
8
+ const memoryUsage = process.memoryUsage(); // info in bytes
9
+ logger.info("memory usage", {
10
+ /**
11
+ * Resident Set Size, the total memory allocated for the whole process.
12
+ * In Railway, this is what you pay for.
13
+ *
14
+ * It is basically the sum of heapTotal + external of all threads (workers).
15
+ */
16
+ rss: bytesToMb(memoryUsage.rss),
17
+ /**
18
+ * Available heap for the current thread
19
+ */
20
+ heapTotal: bytesToMb(memoryUsage.heapTotal),
21
+ /**
22
+ * Heap used for the current thread
23
+ */
24
+ heapUsed: bytesToMb(memoryUsage.heapUsed),
25
+ /**
26
+ * Memory used by C++ for the current thread, including arrayBuffers
27
+ */
28
+ external: bytesToMb(memoryUsage.external),
29
+ /**
30
+ * ArrayBuffers used for the current thread
31
+ */
32
+ arrayBuffers: bytesToMb(memoryUsage.arrayBuffers),
33
+ });
34
+ },
35
+ };
36
+ function executionTime(label, action, logger = console) {
37
+ const startedAt = performance.now();
38
+ const result = action();
39
+ if (result instanceof Promise) {
40
+ return result.finally(() => {
41
+ logExecutionTime(label, startedAt, logger);
42
+ });
43
+ }
44
+ logExecutionTime(label, startedAt, logger);
45
+ return result;
46
+ }
47
+ function logExecutionTime(label, startedAt, logger) {
48
+ logger.info(`${label}: ${(performance.now() - startedAt).toFixed(2)}ms`);
49
+ }
50
+ function bytesToMb(bytes) {
51
+ return `${Math.round(bytes / 1024 / 1024).toFixed(2)} MB`;
52
+ }
53
+ //# sourceMappingURL=Measure.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Measure.js","sourceRoot":"","sources":["../src/Measure.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,+GAA+G;IAC/G,aAAa;IAEb;;OAEG;IACH,WAAW,CAAC,SAAsB,OAAO;QACvC,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,EAAE,CAAA,CAAC,gBAAgB;QAE1D,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE;YAC1B;;;;;eAKG;YACH,GAAG,EAAE,SAAS,CAAC,WAAW,CAAC,GAAG,CAAC;YAC/B;;eAEG;YACH,SAAS,EAAE,SAAS,CAAC,WAAW,CAAC,SAAS,CAAC;YAC3C;;eAEG;YACH,QAAQ,EAAE,SAAS,CAAC,WAAW,CAAC,QAAQ,CAAC;YACzC;;eAEG;YACH,QAAQ,EAAE,SAAS,CAAC,WAAW,CAAC,QAAQ,CAAC;YACzC;;eAEG;YACH,YAAY,EAAE,SAAS,CAAC,WAAW,CAAC,YAAY,CAAC;SAClD,CAAC,CAAA;IACJ,CAAC;CACF,CAAA;AAYD,SAAS,aAAa,CAAI,KAAa,EAAE,MAAsC,EAAE,SAAsB,OAAO;IAC5G,MAAM,SAAS,GAAG,WAAW,CAAC,GAAG,EAAE,CAAA;IACnC,MAAM,MAAM,GAAG,MAAM,EAAE,CAAA;IACvB,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;QAC9B,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE;YACzB,gBAAgB,CAAC,KAAK,EAAE,SAAS,EAAE,MAAM,CAAC,CAAA;QAC5C,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,gBAAgB,CAAC,KAAK,EAAE,SAAS,EAAE,MAAM,CAAC,CAAA;IAC1C,OAAO,MAAM,CAAA;AACf,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAa,EAAE,SAAiB,EAAE,MAAmB;IAC7E,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;AAC1E,CAAC;AAED,SAAS,SAAS,CAAC,KAAa;IAC9B,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAA;AAC3D,CAAC"}
package/dist/Result.js CHANGED
@@ -32,7 +32,7 @@ export const Result = {
32
32
  isFailure: (maybeFailure) => {
33
33
  return maybeFailure.type === "Failure";
34
34
  },
35
- // You can overload while defining object properties, which is why the function implementation is not inlined
35
+ // You can't overload while defining object properties, which is why the function implementation is not inlined
36
36
  tryCatch,
37
37
  };
38
38
  function tryCatch(fn) {
@@ -1 +1 @@
1
- {"version":3,"file":"Result.js","sourceRoot":"","sources":["../src/Result.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAkB7C;;;;;;;;;;;GAWG;AAEH,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB;;OAEG;IACH,OAAO,EAAE,CAAS,KAAa,EAAmB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KA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IACH,OAAO,EAAE,CAAS,KAAa,EAAmB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAEjF;;OAEG;IACH,SAAS,EAAE,CAAS,YAAqC,EAAmC,EAAE;QAC5F,OAAO,YAAY,CAAC,IAAI,KAAK,SAAS,CAAA;IACxC,CAAC;IAED;;OAEG;IACH,OAAO,EAAE,CAAS,KAAa,EAAmB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAEjF;;OAEG;IACH,SAAS,EAAE,CAAS,YAAqC,EAAmC,EAAE;QAC5F,OAAO,YAAY,CAAC,IAAI,KAAK,SAAS,CAAA;IACxC,CAAC;IAED,+GAA+G;IAC/G,QAAQ;CACT,CAAA;AAoBD,SAAS,QAAQ,CAAI,EAAwB;IAC3C,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,EAAE,EAAE,CAAA;QAEnB,iBAAiB;QACjB,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;YAC9B,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;gBACjD,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;gBACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;YAC9B,CAAC,CAAC,CAAA;QACJ,CAAC;QAED,gBAAgB;QAChB,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IAC/B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;AACH,CAAC"}
@@ -8,6 +8,7 @@ export type * from "./utility-types.js";
8
8
  export { asArray } from "./asArray.js";
9
9
  export { Assert } from "./Assert.js";
10
10
  export { Result, type Success, type Failure } from "./Result.js";
11
+ export { Measure } from "./Measure.js";
11
12
  export { TypeGuard } from "./TypeGuard.js";
12
13
  export { setTimeoutAsync } from "./setTimeoutAsync.js";
13
14
  export { noop, asyncNoop } from "./noop.js";
@@ -7,6 +7,7 @@ export { getEnumKey } from "./getEnumKey.js";
7
7
  export { asArray } from "./asArray.js";
8
8
  export { Assert } from "./Assert.js";
9
9
  export { Result } from "./Result.js";
10
+ export { Measure } from "./Measure.js";
10
11
  export { TypeGuard } from "./TypeGuard.js";
11
12
  export { setTimeoutAsync } from "./setTimeoutAsync.js";
12
13
  export { noop, asyncNoop } from "./noop.js";
@@ -1 +1 @@
1
- {"version":3,"file":"entry.tools-ts.js","sourceRoot":"","sources":["../src/entry.tools-ts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACnD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAA;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAI5C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,MAAM,EAA8B,MAAM,aAAa,CAAA;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAE3C,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAC5C,OAAO,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAA;AAClE,OAAO,EAAE,iBAAiB,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AAM,eAAe,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guillaume-docquier/tools-ts",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "description": "My personal collection of typescript tools",
5
5
  "homepage": "https://github.com/Guillaume-Docquier/tools-ts",
6
6
  "repository": {