@gkucmierz/utils 2.1.0 → 2.3.1

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.
package/README.md CHANGED
@@ -13,10 +13,6 @@ A collection of useful utility functions and data structures for solving algorit
13
13
  npm install @gkucmierz/utils
14
14
  ```
15
15
 
16
- ## 🚀 What's New in v2.0.0
17
- - **Combinatorics Arsenal**: High-performance, memory-safe O(1) generators powered by native `BigInt`.
18
- - **OOP Encapsulation**: `Heap` and `Trie` data structures were refactored into robust ES6 Classes (`new Heap()`, `new Trie()`).
19
-
20
16
  ## ✨ Features
21
17
 
22
18
  This library provides a wide range of mathematical functions and data structures, including:
@@ -47,6 +43,7 @@ This library provides a wide range of mathematical functions and data structures
47
43
  - `gpn`: Generalized Pentagonal Numbers.
48
44
  - `heronsFormula`: Triangle area calculation.
49
45
  - `squareRoot`: Integer square root using Newton's method.
46
+ - `goldenRatio`, `goldenRatioBI`: The Golden Ratio calculation (with arbitrary precision support).
50
47
 
51
48
  - **Automata & Simulation**:
52
49
  - `createLangtonsAnt`, `createUnlimitedGrid`: Infinite 2D grid and Langton's Ant cellular automaton engine (Project Euler 349 compatible).
@@ -58,6 +55,7 @@ This library provides a wide range of mathematical functions and data structures
58
55
  - `bijectiveNumeration`: Bijective base-k numeration system.
59
56
 
60
57
  - **Developer Utilities**:
58
+ - `measurePerformance`: A micro-benchmarking tool for high-iteration performance testing.
61
59
  - `setSafeInterval`: A safe, asynchronous alternative to `setInterval` that prevents overlapping executions and supports immediate yielding.
62
60
  - `consumeIteratorNonBlocking`: Yield consumption macro-tasks queue resolver for heavy computations.
63
61
  - `memoize`: Function memoization based on arguments.
package/main.mjs CHANGED
@@ -41,6 +41,9 @@ import {
41
41
  import {
42
42
  getType
43
43
  } from './src/get-type.mjs'
44
+ import {
45
+ goldenRatio, goldenRatioBI
46
+ } from './src/golden-ratio.mjs'
44
47
  import {
45
48
  gpn, gpnBI
46
49
  } from './src/gpn.mjs'
@@ -71,6 +74,9 @@ import {
71
74
  import {
72
75
  matrixAsArray
73
76
  } from './src/matrix.mjs'
77
+ import {
78
+ measurePerformance
79
+ } from './src/measure-performance.mjs'
74
80
  import {
75
81
  memoize
76
82
  } from './src/memoize.mjs'
@@ -131,6 +137,7 @@ export * from './src/factors.mjs';
131
137
  export * from './src/format-big-number.mjs';
132
138
  export * from './src/gcd.mjs';
133
139
  export * from './src/get-type.mjs';
140
+ export * from './src/golden-ratio.mjs';
134
141
  export * from './src/gpn.mjs';
135
142
  export * from './src/gray-code.mjs';
136
143
  export * from './src/heap.mjs';
@@ -141,6 +148,7 @@ export * from './src/list-node.mjs';
141
148
  export * from './src/lucas-lehmer.mjs';
142
149
  export * from './src/math3d.mjs';
143
150
  export * from './src/matrix.mjs';
151
+ export * from './src/measure-performance.mjs';
144
152
  export * from './src/memoize.mjs';
145
153
  export * from './src/mod.mjs';
146
154
  export * from './src/n-choose-k.mjs';
@@ -158,5 +166,5 @@ export * from './src/square-root.mjs';
158
166
  export * from './src/tonelli-shanks.mjs';
159
167
 
160
168
  export default [
161
- SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, createLangtonsAnt, createUnlimitedGrid, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
169
+ SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, goldenRatio, goldenRatioBI, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, createLangtonsAnt, createUnlimitedGrid, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, measurePerformance, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
162
170
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "2.1.0",
3
+ "version": "2.3.1",
4
4
  "type": "module",
5
5
  "description": "Usefull functions for solving programming tasks",
6
6
  "keywords": [
@@ -9,11 +9,13 @@
9
9
  "base64",
10
10
  "bijective",
11
11
  "binary-search",
12
+ "benchmark",
12
13
  "combinations",
13
14
  "competitive-programming",
14
15
  "data-structures",
15
16
  "factors",
16
17
  "gcd",
18
+ "golden-ratio",
17
19
  "gray-code",
18
20
  "heap",
19
21
  "iterator",
@@ -0,0 +1,38 @@
1
+ import { squareRootBI } from './square-root.mjs';
2
+
3
+ /**
4
+ * The standard float approximation of the Golden Ratio (phi).
5
+ * @constant {number}
6
+ */
7
+ export const goldenRatio = ((5 ** 0.5) + 1) / 2;
8
+
9
+ /**
10
+ * Calculates the Golden Ratio (phi) to an arbitrary precision using BigInt.
11
+ * Returns a boxed BigInt object with an overridden toString() method to
12
+ * automatically format the output with the correct decimal point placement.
13
+ *
14
+ * @param {number|bigint} precision - The number of decimal places to calculate.
15
+ * @returns {BigInt} A boxed BigInt representing the Golden Ratio scaled by 10^precision.
16
+ * @throws {Error} If precision is negative.
17
+ */
18
+ export const goldenRatioBI = precision => {
19
+ if (precision < 0) throw new Error('Precision cannot be negative');
20
+ const p = BigInt(precision);
21
+ const exp = 10n ** p;
22
+
23
+ // (sqrt(5 * exp^2) + exp) / 2
24
+ const result = (squareRootBI(5n * exp * exp) + exp) / 2n;
25
+
26
+ // Wrap primitive BigInt in an Object (Boxing)
27
+ const boxedResult = Object(result);
28
+ boxedResult.toString = () => {
29
+ const str = result.toString();
30
+ if (precision === 0) return str;
31
+
32
+ // The Golden Ratio is ~1.618, so the integer part is always '1'.
33
+ // The length of str is exactly precision + 1.
34
+ return str.slice(0, 1) + '.' + str.slice(1);
35
+ };
36
+
37
+ return boxedResult;
38
+ };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Measures the execution time of a given function by running it multiple times.
3
+ * Useful for micro-benchmarking and performance comparison.
4
+ *
5
+ * @param {Function} fn - The function to benchmark.
6
+ * @param {number} [steps=1e8] - The number of iterations to run the function.
7
+ * @param {string} [memo='noop'] - A descriptive label for the benchmark result.
8
+ * @returns {Object} An object containing the `memo` and the `duration` (in milliseconds).
9
+ */
10
+ export const measurePerformance = (fn, steps = 1e8, memo = 'noop') => {
11
+ const t1 = Date.now();
12
+ while (steps--) {
13
+ fn();
14
+ }
15
+ const t2 = Date.now();
16
+
17
+ return {
18
+ memo,
19
+ duration: t2 - t1
20
+ };
21
+ };