cmpstr 3.0.3 → 3.1.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.
- package/README.md +2 -1
- package/dist/CmpStr.esm.js +2487 -4948
- package/dist/CmpStr.esm.js.map +1 -1
- package/dist/CmpStr.esm.min.js +3 -3
- package/dist/CmpStr.esm.min.js.map +1 -1
- package/dist/CmpStr.umd.js +2601 -5040
- package/dist/CmpStr.umd.js.map +1 -1
- package/dist/CmpStr.umd.min.js +3 -3
- package/dist/CmpStr.umd.min.js.map +1 -1
- package/dist/cjs/CmpStr.cjs +41 -405
- package/dist/cjs/CmpStr.cjs.map +1 -1
- package/dist/cjs/CmpStrAsync.cjs +38 -221
- package/dist/cjs/CmpStrAsync.cjs.map +1 -1
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/metric/Cosine.cjs +1 -56
- package/dist/cjs/metric/Cosine.cjs.map +1 -1
- package/dist/cjs/metric/DamerauLevenshtein.cjs +2 -64
- package/dist/cjs/metric/DamerauLevenshtein.cjs.map +1 -1
- package/dist/cjs/metric/DiceSorensen.cjs +1 -56
- package/dist/cjs/metric/DiceSorensen.cjs.map +1 -1
- package/dist/cjs/metric/Hamming.cjs +2 -51
- package/dist/cjs/metric/Hamming.cjs.map +1 -1
- package/dist/cjs/metric/Jaccard.cjs +1 -48
- package/dist/cjs/metric/Jaccard.cjs.map +1 -1
- package/dist/cjs/metric/JaroWinkler.cjs +1 -53
- package/dist/cjs/metric/JaroWinkler.cjs.map +1 -1
- package/dist/cjs/metric/LCS.cjs +1 -54
- package/dist/cjs/metric/LCS.cjs.map +1 -1
- package/dist/cjs/metric/Levenshtein.cjs +2 -60
- package/dist/cjs/metric/Levenshtein.cjs.map +1 -1
- package/dist/cjs/metric/Metric.cjs +1 -261
- package/dist/cjs/metric/Metric.cjs.map +1 -1
- package/dist/cjs/metric/NeedlemanWunsch.cjs +4 -56
- package/dist/cjs/metric/NeedlemanWunsch.cjs.map +1 -1
- package/dist/cjs/metric/SmithWaterman.cjs +4 -58
- package/dist/cjs/metric/SmithWaterman.cjs.map +1 -1
- package/dist/cjs/metric/qGram.cjs +1 -55
- package/dist/cjs/metric/qGram.cjs.map +1 -1
- package/dist/cjs/phonetic/Caverphone.cjs +1 -78
- package/dist/cjs/phonetic/Caverphone.cjs.map +1 -1
- package/dist/cjs/phonetic/Cologne.cjs +1 -43
- package/dist/cjs/phonetic/Cologne.cjs.map +1 -1
- package/dist/cjs/phonetic/Metaphone.cjs +1 -76
- package/dist/cjs/phonetic/Metaphone.cjs.map +1 -1
- package/dist/cjs/phonetic/Phonetic.cjs +1 -261
- package/dist/cjs/phonetic/Phonetic.cjs.map +1 -1
- package/dist/cjs/phonetic/Soundex.cjs +1 -47
- package/dist/cjs/phonetic/Soundex.cjs.map +1 -1
- package/dist/cjs/root.cjs +49 -0
- package/dist/cjs/root.cjs.map +1 -0
- package/dist/cjs/utils/DeepMerge.cjs +8 -75
- package/dist/cjs/utils/DeepMerge.cjs.map +1 -1
- package/dist/cjs/utils/DiffChecker.cjs +2 -190
- package/dist/cjs/utils/DiffChecker.cjs.map +1 -1
- package/dist/cjs/utils/Filter.cjs +1 -112
- package/dist/cjs/utils/Filter.cjs.map +1 -1
- package/dist/cjs/utils/HashTable.cjs +1 -99
- package/dist/cjs/utils/HashTable.cjs.map +1 -1
- package/dist/cjs/utils/Normalizer.cjs +3 -94
- package/dist/cjs/utils/Normalizer.cjs.map +1 -1
- package/dist/cjs/utils/Pool.cjs +10 -105
- package/dist/cjs/utils/Pool.cjs.map +1 -1
- package/dist/cjs/utils/Profiler.cjs +1 -133
- package/dist/cjs/utils/Profiler.cjs.map +1 -1
- package/dist/cjs/utils/Registry.cjs +1 -89
- package/dist/cjs/utils/Registry.cjs.map +1 -1
- package/dist/cjs/utils/StructuredData.cjs +145 -0
- package/dist/cjs/utils/StructuredData.cjs.map +1 -0
- package/dist/cjs/utils/TextAnalyzer.cjs +1 -180
- package/dist/cjs/utils/TextAnalyzer.cjs.map +1 -1
- package/dist/esm/CmpStr.mjs +41 -405
- package/dist/esm/CmpStr.mjs.map +1 -1
- package/dist/esm/CmpStrAsync.mjs +38 -221
- package/dist/esm/CmpStrAsync.mjs.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/metric/Cosine.mjs +1 -56
- package/dist/esm/metric/Cosine.mjs.map +1 -1
- package/dist/esm/metric/DamerauLevenshtein.mjs +2 -64
- package/dist/esm/metric/DamerauLevenshtein.mjs.map +1 -1
- package/dist/esm/metric/DiceSorensen.mjs +1 -56
- package/dist/esm/metric/DiceSorensen.mjs.map +1 -1
- package/dist/esm/metric/Hamming.mjs +2 -51
- package/dist/esm/metric/Hamming.mjs.map +1 -1
- package/dist/esm/metric/Jaccard.mjs +1 -48
- package/dist/esm/metric/Jaccard.mjs.map +1 -1
- package/dist/esm/metric/JaroWinkler.mjs +1 -53
- package/dist/esm/metric/JaroWinkler.mjs.map +1 -1
- package/dist/esm/metric/LCS.mjs +1 -54
- package/dist/esm/metric/LCS.mjs.map +1 -1
- package/dist/esm/metric/Levenshtein.mjs +2 -60
- package/dist/esm/metric/Levenshtein.mjs.map +1 -1
- package/dist/esm/metric/Metric.mjs +1 -261
- package/dist/esm/metric/Metric.mjs.map +1 -1
- package/dist/esm/metric/NeedlemanWunsch.mjs +4 -56
- package/dist/esm/metric/NeedlemanWunsch.mjs.map +1 -1
- package/dist/esm/metric/SmithWaterman.mjs +4 -58
- package/dist/esm/metric/SmithWaterman.mjs.map +1 -1
- package/dist/esm/metric/qGram.mjs +1 -55
- package/dist/esm/metric/qGram.mjs.map +1 -1
- package/dist/esm/phonetic/Caverphone.mjs +1 -78
- package/dist/esm/phonetic/Caverphone.mjs.map +1 -1
- package/dist/esm/phonetic/Cologne.mjs +1 -43
- package/dist/esm/phonetic/Cologne.mjs.map +1 -1
- package/dist/esm/phonetic/Metaphone.mjs +1 -76
- package/dist/esm/phonetic/Metaphone.mjs.map +1 -1
- package/dist/esm/phonetic/Phonetic.mjs +1 -261
- package/dist/esm/phonetic/Phonetic.mjs.map +1 -1
- package/dist/esm/phonetic/Soundex.mjs +1 -47
- package/dist/esm/phonetic/Soundex.mjs.map +1 -1
- package/dist/esm/root.mjs +35 -0
- package/dist/esm/root.mjs.map +1 -0
- package/dist/esm/utils/DeepMerge.mjs +8 -76
- package/dist/esm/utils/DeepMerge.mjs.map +1 -1
- package/dist/esm/utils/DiffChecker.mjs +2 -190
- package/dist/esm/utils/DiffChecker.mjs.map +1 -1
- package/dist/esm/utils/Filter.mjs +1 -112
- package/dist/esm/utils/Filter.mjs.map +1 -1
- package/dist/esm/utils/HashTable.mjs +1 -99
- package/dist/esm/utils/HashTable.mjs.map +1 -1
- package/dist/esm/utils/Normalizer.mjs +3 -94
- package/dist/esm/utils/Normalizer.mjs.map +1 -1
- package/dist/esm/utils/Pool.mjs +10 -105
- package/dist/esm/utils/Pool.mjs.map +1 -1
- package/dist/esm/utils/Profiler.mjs +1 -133
- package/dist/esm/utils/Profiler.mjs.map +1 -1
- package/dist/esm/utils/Registry.mjs +1 -89
- package/dist/esm/utils/Registry.mjs.map +1 -1
- package/dist/esm/utils/StructuredData.mjs +143 -0
- package/dist/esm/utils/StructuredData.mjs.map +1 -0
- package/dist/esm/utils/TextAnalyzer.mjs +1 -180
- package/dist/esm/utils/TextAnalyzer.mjs.map +1 -1
- package/dist/types/CmpStr.d.ts +90 -8
- package/dist/types/CmpStrAsync.d.ts +82 -8
- package/dist/types/index.d.ts +3 -2
- package/dist/types/root.d.ts +39 -0
- package/dist/types/utils/Pool.d.ts +2 -2
- package/dist/types/utils/StructuredData.d.ts +162 -0
- package/dist/types/utils/Types.d.ts +35 -1
- package/package.json +63 -22
package/dist/cjs/utils/Pool.cjs
CHANGED
|
@@ -1,103 +1,39 @@
|
|
|
1
|
-
// CmpStr v3.0
|
|
1
|
+
// CmpStr v3.1.0 build-76aadb9-260117 by Paul Köhler @komed3 / MIT License
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Pool Utility
|
|
6
|
-
* src/utils/Pool.ts
|
|
7
|
-
*
|
|
8
|
-
* @see https://en.wikipedia.org/wiki/Circular_buffer
|
|
9
|
-
*
|
|
10
|
-
* The Pool class provides a simple and efficient buffer pool for dynamic programming
|
|
11
|
-
* algorithms that require temporary arrays (such as Levenshtein, LCS, etc.).
|
|
12
|
-
* By reusing pre-allocated typed arrays, it reduces memory allocations and garbage
|
|
13
|
-
* collection overhead, especially for repeated or batch computations.
|
|
14
|
-
*
|
|
15
|
-
* It supports different types of buffers (Uint16Array, number[], Set, Map) and allows
|
|
16
|
-
* for acquiring buffers of specific sizes while managing a maximum pool size.
|
|
17
|
-
*
|
|
18
|
-
* @module Utils/Pool
|
|
19
|
-
* @author Paul Köhler (komed3)
|
|
20
|
-
* @license MIT
|
|
21
|
-
*/
|
|
22
|
-
/**
|
|
23
|
-
* RingPool is a circular buffer implementation that manages a pool of buffers.
|
|
24
|
-
*
|
|
25
|
-
* It allows for efficient acquisition and release of buffers, ensuring that
|
|
26
|
-
* buffers are reused without unnecessary allocations.
|
|
27
|
-
*
|
|
28
|
-
* @template T - The type of buffers managed by the pool
|
|
29
|
-
*/
|
|
30
4
|
class RingPool {
|
|
31
5
|
maxSize;
|
|
32
|
-
// The buffers in the pool
|
|
33
6
|
buffers = [];
|
|
34
|
-
// The current pointer for acquiring buffers
|
|
35
7
|
pointer = 0;
|
|
36
|
-
/**
|
|
37
|
-
* Creates a new RingPool with a specified maximum size.
|
|
38
|
-
*
|
|
39
|
-
* @param {number} maxSize - The maximum number of buffers that can be stored in the pool
|
|
40
|
-
*/
|
|
41
8
|
constructor(maxSize) {
|
|
42
9
|
this.maxSize = maxSize;
|
|
43
10
|
}
|
|
44
|
-
/**
|
|
45
|
-
* Acquires a buffer of at least the specified minimum size from the pool.
|
|
46
|
-
*
|
|
47
|
-
* @param {number} minSize - The minimum size of the buffer to acquire
|
|
48
|
-
* @param {boolean} allowOversize - Whether to allow buffers larger than minSize
|
|
49
|
-
* @return {PoolBuffer<T>|null} - The acquired buffer or null if no suitable buffer is found
|
|
50
|
-
*/
|
|
51
11
|
acquire(minSize, allowOversize) {
|
|
52
12
|
const len = this.buffers.length;
|
|
53
|
-
// Iterate through the buffers in the pool
|
|
54
13
|
for (let i = 0; i < len; i++) {
|
|
55
14
|
const idx = (this.pointer + i) % len;
|
|
56
15
|
const item = this.buffers[idx];
|
|
57
|
-
// Check if the item size is greater than or equal to the minimum size
|
|
58
16
|
if (item.size >= minSize) {
|
|
59
|
-
// Set the pointer to the next position
|
|
60
17
|
this.pointer = (idx + 1) % len;
|
|
61
|
-
// If the item size is equal to minSize or oversize is allowed, return the item
|
|
62
18
|
return allowOversize || item.size === minSize ? item : null;
|
|
63
19
|
}
|
|
64
20
|
}
|
|
65
|
-
// If no suitable buffer is found, return null
|
|
66
21
|
return null;
|
|
67
22
|
}
|
|
68
|
-
/**
|
|
69
|
-
* Releases a buffer back to the pool.
|
|
70
|
-
* If the pool is full, it replaces the oldest buffer with the new one.
|
|
71
|
-
*
|
|
72
|
-
* @param {PoolBuffer<T>} item - The buffer to release back to the pool
|
|
73
|
-
*/
|
|
74
23
|
release(item) {
|
|
75
24
|
if (this.buffers.length < this.maxSize) {
|
|
76
|
-
// If the pool is not full, simply add the item
|
|
77
25
|
this.buffers.push(item);
|
|
78
26
|
} else {
|
|
79
|
-
// If the pool is full, replace the oldest buffer
|
|
80
27
|
this.buffers[this.pointer] = item;
|
|
81
28
|
this.pointer = (this.pointer + 1) % this.maxSize;
|
|
82
29
|
}
|
|
83
30
|
}
|
|
84
|
-
/**
|
|
85
|
-
* Clears the pool, removing all buffers.
|
|
86
|
-
* This resets the pointer and empties the buffer list.
|
|
87
|
-
*/
|
|
88
31
|
clear() {
|
|
89
32
|
this.buffers = [];
|
|
90
33
|
this.pointer = 0;
|
|
91
34
|
}
|
|
92
35
|
}
|
|
93
|
-
/**
|
|
94
|
-
* The Pool class provides a buffer pool for dynamic programming algorithms.
|
|
95
|
-
*
|
|
96
|
-
* It allows for efficient reuse of buffers (Uint16Array, number[], Set, Map)
|
|
97
|
-
* to reduce memory allocations and garbage collection overhead.
|
|
98
|
-
*/
|
|
99
36
|
class Pool {
|
|
100
|
-
// Pool Types
|
|
101
37
|
static CONFIG = {
|
|
102
38
|
uint16: {
|
|
103
39
|
type: 'uint16',
|
|
@@ -111,82 +47,51 @@ class Pool {
|
|
|
111
47
|
maxItemSize: 1024,
|
|
112
48
|
allowOversize: false
|
|
113
49
|
},
|
|
50
|
+
'string[]': {
|
|
51
|
+
type: 'string[]',
|
|
52
|
+
maxSize: 4,
|
|
53
|
+
maxItemSize: 1024,
|
|
54
|
+
allowOversize: false
|
|
55
|
+
},
|
|
114
56
|
set: { type: 'set', maxSize: 8, maxItemSize: 0, allowOversize: false },
|
|
115
57
|
map: { type: 'map', maxSize: 8, maxItemSize: 0, allowOversize: false }
|
|
116
58
|
};
|
|
117
|
-
// Pool Rings for each type
|
|
118
59
|
static POOLS = {
|
|
119
60
|
uint16: new RingPool(32),
|
|
120
61
|
'number[]': new RingPool(16),
|
|
62
|
+
'string[]': new RingPool(4),
|
|
121
63
|
set: new RingPool(8),
|
|
122
64
|
map: new RingPool(8)
|
|
123
65
|
};
|
|
124
|
-
/**
|
|
125
|
-
* Allocates a new buffer of the specified type and size.
|
|
126
|
-
*
|
|
127
|
-
* @param {PoolType} type - The type of buffer to allocate
|
|
128
|
-
* @param {number} size - The size of the buffer to allocate
|
|
129
|
-
* @return {any} - The newly allocated buffer
|
|
130
|
-
*/
|
|
131
66
|
static allocate(type, size) {
|
|
132
67
|
switch (type) {
|
|
133
68
|
case 'uint16':
|
|
134
69
|
return new Uint16Array(size);
|
|
135
70
|
case 'number[]':
|
|
136
71
|
return new Array(size).fill(0);
|
|
72
|
+
case 'string[]':
|
|
73
|
+
return new Array(size);
|
|
137
74
|
case 'set':
|
|
138
75
|
return new Set();
|
|
139
76
|
case 'map':
|
|
140
77
|
return new Map();
|
|
141
78
|
}
|
|
142
79
|
}
|
|
143
|
-
/**
|
|
144
|
-
* Acquires a buffer of the specified type and size from the pool.
|
|
145
|
-
* If no suitable buffer is available, it allocates a new one.
|
|
146
|
-
*
|
|
147
|
-
* @param {PoolType} type - The type of buffer to acquire (e.g., 'uint16', 'number[]', 'set', 'map')
|
|
148
|
-
* @param {number} size - The size of the buffer to acquire
|
|
149
|
-
* @return {T} - The acquired buffer of the specified type
|
|
150
|
-
*/
|
|
151
80
|
static acquire(type, size) {
|
|
152
|
-
// Get the configuration for the specified type
|
|
153
81
|
const CONFIG = this.CONFIG[type];
|
|
154
|
-
// If the requested size exceeds the maximum item size, allocate a new buffer
|
|
155
82
|
if (size > CONFIG.maxItemSize) return this.allocate(type, size);
|
|
156
|
-
// Try to acquire a buffer from the pool ring
|
|
157
|
-
// If a suitable buffer is found, return it (subarray for uint16)
|
|
158
83
|
const item = this.POOLS[type].acquire(size, CONFIG.allowOversize);
|
|
159
84
|
if (item) {
|
|
160
|
-
// If the type is 'uint16', return a subarray of the buffer
|
|
161
85
|
return type === 'uint16' ? item.buffer.subarray(0, size) : item.buffer;
|
|
162
86
|
}
|
|
163
|
-
// If no suitable buffer is found, allocate a new one
|
|
164
87
|
return this.allocate(type, size);
|
|
165
88
|
}
|
|
166
|
-
/**
|
|
167
|
-
* Acquires multiple buffers of the specified type and sizes from the pool.
|
|
168
|
-
*
|
|
169
|
-
* @param {PoolType} type - The type of buffers to acquire
|
|
170
|
-
* @param {number[]} sizes - An array of sizes for each buffer to acquire
|
|
171
|
-
* @return {T[]} - An array of acquired buffers of the specified type
|
|
172
|
-
*/
|
|
173
89
|
static acquireMany(type, sizes) {
|
|
174
90
|
return sizes.map((size) => this.acquire(type, size));
|
|
175
91
|
}
|
|
176
|
-
/**
|
|
177
|
-
* Releases a buffer back to the pool.
|
|
178
|
-
* If the size of the buffer is larger than the maximum item size, it will not be released.
|
|
179
|
-
*
|
|
180
|
-
* @param {PoolType} type - The type of buffer to release
|
|
181
|
-
* @param {T} buffer - The buffer to release
|
|
182
|
-
* @param {number} size - The size of the buffer
|
|
183
|
-
*/
|
|
184
92
|
static release(type, buffer, size) {
|
|
185
|
-
// Get the configuration for the specified type
|
|
186
93
|
const CONFIG = this.CONFIG[type];
|
|
187
|
-
// If the size of the buffer is less than or equal to the maximum item size, release it
|
|
188
94
|
if (size <= CONFIG.maxItemSize) {
|
|
189
|
-
// Release the buffer back to the pool ring
|
|
190
95
|
this.POOLS[type].release({ buffer, size });
|
|
191
96
|
}
|
|
192
97
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Pool.cjs","sources":["../../../src/utils/Pool.ts"],"sourcesContent":[null],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"Pool.cjs","sources":["../../../src/utils/Pool.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AA+BA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAM,QAAQ,CAAA;AAaqB,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;EAVvB,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,GAAoB,CAAA,CAAE;EAG7B,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,GAAW,CAAC;AAO3B,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAA+B,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAe,CAAA,CAAA;IAAf,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,CAAA,CAAA,CAAP,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO;EAAY;EAS3C,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,CAAG,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAe,CAAA,CAAE,aAAsB,CAAA,CAAA;AAEpD,CAAA,CAAA,CAAA,CAAA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A,CAAI,CAAC,OAAO,CAAK,CAAA,CAAA,CAAA,CAAI,CAAA,CAAE,CAAA,CAAA,CAAA,CAAI,CAAE,CAAE;EAE7D;AAUO,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,OAAO,CAAY,CAAA,CAAA,CAAA,CAAc,EAAE,CAAA,CAAA,CAAA,CAAA,CAAA,CAAS,CAAA,CAAE,IAAY,CAAA,CAAA;IAGpE,CAAA,CAAA,CAAA,CAAA,CAAA,CAAM,MAAM,CAAA,CAAA,CAAe,CAAA,CAAA,CAAA,CAAI,CAAC,CAAA,CAAA,CAAA,CAAA,CAAA,CAAM,CAAE,IAAI,CAAE;AAG9C,CAAA,CAAA,CAAA,CAAA,IAAK,CAAA,CAAA,CAAA,CAAI,CAAA,CAAA,CAAA,CAAI,CAAA,CAAA,CAAA,CAAA,CAAA,CAAM,CAAC,WAAW,CAAA,CAAG;AAG9B,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAI,CAAC,CAAA,CAAA,CAAA,CAAA,CAAK,CAAE,IAAI,CAAE,CAAC,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,CAAE,CAAA,CAAE,CAAA,CAAA,CAAA,CAAA,CAAA,CAAM,CAAA,CAAE,CAAA,CAAA,CAAA,CAAI,EAAE,CAAE;IAElD;EAEJ;;;;"}
|
|
@@ -1,220 +1,88 @@
|
|
|
1
|
-
// CmpStr v3.0
|
|
1
|
+
// CmpStr v3.1.0 build-76aadb9-260117 by Paul Köhler @komed3 / MIT License
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Profiler Utility
|
|
6
|
-
* src/utils/profiler.ts
|
|
7
|
-
*
|
|
8
|
-
* @see https://en.wikipedia.org/wiki/Profiling_(computer_programming)
|
|
9
|
-
*
|
|
10
|
-
* This class provides methods to run synchronous and asynchronous functions, capturing
|
|
11
|
-
* their execution time and memory usage, and storing the results in a set of profiler
|
|
12
|
-
* entries. It supports both Node.js and browser environments, detecting the environment
|
|
13
|
-
* automatically.
|
|
14
|
-
*
|
|
15
|
-
* The class is optimized for minimal overhead and can be used for fine-grained
|
|
16
|
-
* performance profiling.
|
|
17
|
-
*
|
|
18
|
-
* @module Utils/Profiler
|
|
19
|
-
* @author Paul Köhler (komed3)
|
|
20
|
-
* @license MIT
|
|
21
|
-
*/
|
|
22
|
-
/**
|
|
23
|
-
* Profiler class for measuring execution time and memory usage of functions.
|
|
24
|
-
*/
|
|
25
4
|
class Profiler {
|
|
26
|
-
// Environment detection
|
|
27
5
|
static ENV;
|
|
28
|
-
// Singleton instance
|
|
29
6
|
static instance;
|
|
30
|
-
// Store for profiler entries
|
|
31
7
|
store = new Set();
|
|
32
|
-
// Total time and memory consumption
|
|
33
8
|
totalTime = 0;
|
|
34
9
|
totalMem = 0;
|
|
35
|
-
// The Profiler active state
|
|
36
10
|
active;
|
|
37
|
-
/**
|
|
38
|
-
* Sets the environment based on the available global objects.
|
|
39
|
-
* Detects if running in Node.js or browser and sets the ENV property accordingly.
|
|
40
|
-
*/
|
|
41
11
|
static detectEnv() {
|
|
42
|
-
// Check for Node.js environment
|
|
43
12
|
if (typeof process !== 'undefined') Profiler.ENV = 'nodejs';
|
|
44
|
-
// Check for browser environment
|
|
45
13
|
else if (typeof performance !== 'undefined') Profiler.ENV = 'browser';
|
|
46
|
-
// If neither, set ENV to unknown
|
|
47
14
|
else Profiler.ENV = 'unknown';
|
|
48
15
|
}
|
|
49
|
-
/**
|
|
50
|
-
* Returns the singleton instance of the Perf class.
|
|
51
|
-
* If the instance does not exist, it creates a new one.
|
|
52
|
-
*
|
|
53
|
-
* @param {boolean} [enable=false] - Optional parameter to enable the profiler upon instantiation
|
|
54
|
-
* @returns {Profiler} - Singleton Profiler instance
|
|
55
|
-
*/
|
|
56
16
|
static getInstance(enable) {
|
|
57
|
-
// Ensure the environment is detected
|
|
58
17
|
if (!Profiler.ENV) Profiler.detectEnv();
|
|
59
|
-
// If instance does not exist, create a new one
|
|
60
18
|
if (!Profiler.instance) Profiler.instance = new Profiler(enable);
|
|
61
|
-
// Return singleton instance
|
|
62
19
|
return Profiler.instance;
|
|
63
20
|
}
|
|
64
|
-
/**
|
|
65
|
-
* Private constructor to enforce singleton pattern.
|
|
66
|
-
* Initializes the store for profiler entries.
|
|
67
|
-
*
|
|
68
|
-
* @param {boolean} [enable=false] - Optional parameter to enable the profiler
|
|
69
|
-
*/
|
|
70
21
|
constructor(enable) {
|
|
71
22
|
this.active = enable ?? false;
|
|
72
23
|
}
|
|
73
|
-
/**
|
|
74
|
-
* Gets the current time based on the environment.
|
|
75
|
-
*
|
|
76
|
-
* Uses process.hrtime.bigint() for Node.js, performance.now() for browsers,
|
|
77
|
-
* and Date.now() as a fallback.
|
|
78
|
-
*
|
|
79
|
-
* @returns {number} - Current time in milliseconds
|
|
80
|
-
*/
|
|
81
24
|
now() {
|
|
82
25
|
switch (Profiler.ENV) {
|
|
83
|
-
// Node.js environment
|
|
84
26
|
case 'nodejs':
|
|
85
27
|
return Number(process.hrtime.bigint()) / 1e6;
|
|
86
|
-
// Browser environment
|
|
87
28
|
case 'browser':
|
|
88
29
|
return performance.now();
|
|
89
|
-
// Fallback
|
|
90
30
|
default:
|
|
91
31
|
return Date.now();
|
|
92
32
|
}
|
|
93
33
|
}
|
|
94
|
-
/**
|
|
95
|
-
* Gets the current memory usage based on the environment.
|
|
96
|
-
*
|
|
97
|
-
* Uses process.memoryUsage().heapUsed for Node.js, performance.memory.usedJSHeapSize
|
|
98
|
-
* for browsers, and returns 0 as a fallback.
|
|
99
|
-
*
|
|
100
|
-
* @returns {number} - Current memory usage in bytes
|
|
101
|
-
*/
|
|
102
34
|
mem() {
|
|
103
35
|
switch (Profiler.ENV) {
|
|
104
|
-
// Node.js environment
|
|
105
36
|
case 'nodejs':
|
|
106
37
|
return process.memoryUsage().heapUsed;
|
|
107
|
-
// Browser environment
|
|
108
38
|
case 'browser':
|
|
109
39
|
return performance.memory?.usedJSHeapSize ?? 0;
|
|
110
|
-
// Fallback
|
|
111
40
|
default:
|
|
112
41
|
return 0;
|
|
113
42
|
}
|
|
114
43
|
}
|
|
115
|
-
/**
|
|
116
|
-
* Enables the profiler.
|
|
117
|
-
* Sets the active state to true, allowing profiling to occur.
|
|
118
|
-
*/
|
|
119
44
|
enable() {
|
|
120
45
|
this.active = true;
|
|
121
46
|
}
|
|
122
|
-
/**
|
|
123
|
-
* Disables the profiler.
|
|
124
|
-
* Sets the active state to false, preventing further profiling.
|
|
125
|
-
*/
|
|
126
47
|
disable() {
|
|
127
48
|
this.active = false;
|
|
128
49
|
}
|
|
129
|
-
/**
|
|
130
|
-
* Resets the profiler by clearing the store, total time and memory consumption.
|
|
131
|
-
* This method is useful for starting a new profiling session.
|
|
132
|
-
*/
|
|
133
50
|
clear() {
|
|
134
51
|
this.store.clear();
|
|
135
52
|
this.totalTime = 0;
|
|
136
53
|
this.totalMem = 0;
|
|
137
54
|
}
|
|
138
|
-
/**
|
|
139
|
-
* Runs a synchronous function and profiles its execution time and memory usage.
|
|
140
|
-
* If the profiler is not active, it simply executes the function without profiling.
|
|
141
|
-
*
|
|
142
|
-
* @param {() => T} fn - Function to be executed and profiled
|
|
143
|
-
* @param {Record<string, any>} meta - Metadata to be associated with the profiling entry
|
|
144
|
-
* @returns {T} - The result of the executed function
|
|
145
|
-
*/
|
|
146
55
|
run(fn, meta = {}) {
|
|
147
|
-
// If the profiler is not active, simply execute the function without profiling
|
|
148
56
|
if (!this.active) return fn();
|
|
149
|
-
// Capture the start time and memory usage
|
|
150
57
|
const startTime = this.now(),
|
|
151
58
|
startMem = this.mem();
|
|
152
|
-
// Execute the function and capture the result
|
|
153
59
|
const res = fn();
|
|
154
|
-
// Calculate the time and memory consumption
|
|
155
60
|
const deltaTime = this.now() - startTime;
|
|
156
61
|
const deltaMem = this.mem() - startMem;
|
|
157
|
-
// Add the profiling entry to the store
|
|
158
62
|
this.store.add({ time: deltaTime, mem: deltaMem, res, meta });
|
|
159
63
|
((this.totalTime += deltaTime), (this.totalMem += deltaMem));
|
|
160
|
-
// Return the result of the function
|
|
161
64
|
return res;
|
|
162
65
|
}
|
|
163
|
-
/**
|
|
164
|
-
* Runs an asynchronous function and profiles its execution time and memory usage.
|
|
165
|
-
* If the profiler is not active, it simply executes the function without profiling.
|
|
166
|
-
*
|
|
167
|
-
* @param {() => Promise<T>} fn - Asynchronous function to be executed and profiled
|
|
168
|
-
* @param {Record<string, any>} meta - Metadata to be associated with the profiling entry
|
|
169
|
-
* @returns {Promise<T>} - A promise that resolves to the result of the executed function
|
|
170
|
-
*/
|
|
171
66
|
async runAsync(fn, meta = {}) {
|
|
172
|
-
// If the profiler is not active, simply execute the function without profiling
|
|
173
67
|
if (!this.active) return await fn();
|
|
174
|
-
// Capture the start time and memory usage
|
|
175
68
|
const startTime = this.now(),
|
|
176
69
|
startMem = this.mem();
|
|
177
|
-
// Execute the asynchronous function and wait for its result
|
|
178
70
|
const res = await fn();
|
|
179
|
-
// Calculate the time and memory consumption
|
|
180
71
|
const deltaTime = this.now() - startTime;
|
|
181
72
|
const deltaMem = this.mem() - startMem;
|
|
182
|
-
// Add the profiling entry to the store
|
|
183
73
|
this.store.add({ time: deltaTime, mem: deltaMem, res, meta });
|
|
184
74
|
((this.totalTime += deltaTime), (this.totalMem += deltaMem));
|
|
185
|
-
// Return the result of the function
|
|
186
75
|
return res;
|
|
187
76
|
}
|
|
188
|
-
/**
|
|
189
|
-
* Retrieves all profiler entries stored in the profiler.
|
|
190
|
-
*
|
|
191
|
-
* @returns {ProfilerEntry<any>[]} - An array of profiler entries
|
|
192
|
-
*/
|
|
193
77
|
getAll() {
|
|
194
78
|
return [...this.store];
|
|
195
79
|
}
|
|
196
|
-
/**
|
|
197
|
-
* Retrieves the last profiler entry stored in the profiler.
|
|
198
|
-
*
|
|
199
|
-
* @returns {ProfilerEntry<any> | undefined} - The last profiler entry or undefined if no entries exist
|
|
200
|
-
*/
|
|
201
80
|
getLast() {
|
|
202
81
|
return this.getAll().pop();
|
|
203
82
|
}
|
|
204
|
-
/**
|
|
205
|
-
* Retrieves the total time and memory consumption recorded by the profiler.
|
|
206
|
-
*
|
|
207
|
-
* @returns {{ time: number, mem: number }} - An object containing total time and memory usage
|
|
208
|
-
*/
|
|
209
83
|
getTotal() {
|
|
210
84
|
return { time: this.totalTime, mem: this.totalMem };
|
|
211
85
|
}
|
|
212
|
-
/**
|
|
213
|
-
* Returns the services provided by the Profiler class.
|
|
214
|
-
* This allows for easy access to the profiler's methods.
|
|
215
|
-
*
|
|
216
|
-
* @returns {ProfilerService<any>} - An object containing methods to control the profiler
|
|
217
|
-
*/
|
|
218
86
|
services = {
|
|
219
87
|
enable: this.enable.bind(this),
|
|
220
88
|
disable: this.disable.bind(this),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Profiler.cjs","sources":["../../../src/utils/Profiler.ts"],"sourcesContent":[null],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"Profiler.cjs","sources":["../../../src/utils/Profiler.ts"],"sourcesContent":[null],"names":[],"mappings":";;;MA0Ba,QAAQ,CAAA;EAGT,OAAO,CAAA,CAAA,CAAG;EAGV,OAAO,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAQ;AAGf,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAK,CAAA,CAAA,CAA4B,CAAA,CAAA,CAAA,CAAI,CAAA,CAAA,CAAG,CAAA,CAAG;EAG3C,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAS,GAAW,CAAC;EACrB,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAQ,GAAW,CAAC;AAGpB,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAM;AAMJ,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,SAAS,CAAA,CAAA,CAAA;IAGtB,CAAA,CAAA,CAA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A,CAAA,CAAA,CAAI,CAAE;IAChC,CAAA,CAAA,CAAA,CAAI,EAAE,CAAA,CAAA,CAAA,CAAI,CAAC,OAAO,CAAC,CAAA,CAAA,CAAA,CAAI,CAAE,CAAA,CAAA,CAAA,CAAI,CAAE;IAC/B,CAAA,CAAA,CAAA,CAAA,CAAK,EAAE,CAAA,CAAA,CAAA,CAAI,CAAC,QAAQ,CAAC,CAAA,CAAA,CAAA,CAAI,CAAE,CAAA,CAAA,CAAA,CAAI;GAClC;AAEJ;;;"}
|
|
@@ -1,60 +1,15 @@
|
|
|
1
|
-
// CmpStr v3.0
|
|
1
|
+
// CmpStr v3.1.0 build-76aadb9-260117 by Paul Köhler @komed3 / MIT License
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Registry Utility
|
|
6
|
-
* src/utils/Registry.ts
|
|
7
|
-
*
|
|
8
|
-
* This module provides a Registry function that allows for registering,
|
|
9
|
-
* removing, checking, getting, and listing class constructors.
|
|
10
|
-
*
|
|
11
|
-
* It is designed to manage class extensions, ensuring that all registered
|
|
12
|
-
* classes extend a specified base constructor.
|
|
13
|
-
*
|
|
14
|
-
* @module Utils/Registry
|
|
15
|
-
* @author Paul Köhler (komed3)
|
|
16
|
-
* @license MIT
|
|
17
|
-
*/
|
|
18
|
-
/**
|
|
19
|
-
* Global registry object to hold multiple registries.
|
|
20
|
-
* Each registry is keyed by a string identifier.
|
|
21
|
-
*
|
|
22
|
-
* @type {Record<string, RegistryService<any>>}
|
|
23
|
-
*/
|
|
24
4
|
const registry = Object.create(null);
|
|
25
|
-
/**
|
|
26
|
-
* Factory object to hold factory functions for creating instances.
|
|
27
|
-
* This is used to create instances of registered classes.
|
|
28
|
-
*
|
|
29
|
-
* @type {Record<string, ( cls: string, ...args: any[] ) => InstanceType<any>>}
|
|
30
|
-
*/
|
|
31
5
|
const factory = Object.create(null);
|
|
32
|
-
/**
|
|
33
|
-
* Registry function to create a service for managing class constructors.
|
|
34
|
-
*
|
|
35
|
-
* @param {string} reg - The name of the registry
|
|
36
|
-
* @param {RegistryConstructor<T>} ctor - The base constructor that all registered classes must extend
|
|
37
|
-
* @returns {RegistryService<T>} - An object with methods to register, remove, check, get, and list classes
|
|
38
|
-
* @throws {Error} If the registry already exists (overwriting is forbidden)
|
|
39
|
-
*/
|
|
40
6
|
function Registry(reg, ctor) {
|
|
41
|
-
// Throws an error if the registry already exists
|
|
42
7
|
if (reg in registry || reg in factory)
|
|
43
8
|
throw new Error(
|
|
44
9
|
`Registry <${reg}> already exists / overwriting is forbidden`
|
|
45
10
|
);
|
|
46
|
-
// Create a registry object to hold class constructors
|
|
47
11
|
const classes = Object.create(null);
|
|
48
12
|
const service = {
|
|
49
|
-
/**
|
|
50
|
-
* Register a new extension of the base class.
|
|
51
|
-
*
|
|
52
|
-
* @param {string} name - The name of the class to register
|
|
53
|
-
* @param {RegistryConstructor<T>} cls - The class constructor
|
|
54
|
-
* @param {boolean} [update=false] - Whether to allow overwriting an existing entry
|
|
55
|
-
* @throws {TypeError} If the class does not extend the base constructor
|
|
56
|
-
* @throws {Error} If the class name already exists and update is false
|
|
57
|
-
*/
|
|
58
13
|
add(name, cls, update = false) {
|
|
59
14
|
if (!(cls.prototype instanceof ctor))
|
|
60
15
|
throw new TypeError(`Class must extend <${reg}>`);
|
|
@@ -64,73 +19,30 @@ function Registry(reg, ctor) {
|
|
|
64
19
|
);
|
|
65
20
|
classes[name] = cls;
|
|
66
21
|
},
|
|
67
|
-
/**
|
|
68
|
-
* Remove a class from the registry.
|
|
69
|
-
*
|
|
70
|
-
* @param {string} name - The name of the class to remove
|
|
71
|
-
*/
|
|
72
22
|
remove(name) {
|
|
73
23
|
delete classes[name];
|
|
74
24
|
},
|
|
75
|
-
/**
|
|
76
|
-
* Check if a class is registered.
|
|
77
|
-
*
|
|
78
|
-
* @param {string} name - The name of the class to check
|
|
79
|
-
* @returns {boolean} - True if the class is registered, false otherwise
|
|
80
|
-
*/
|
|
81
25
|
has(name) {
|
|
82
26
|
return name in classes;
|
|
83
27
|
},
|
|
84
|
-
/**
|
|
85
|
-
* List all registered class names.
|
|
86
|
-
*
|
|
87
|
-
* @returns {string[]} - An array of registered class names
|
|
88
|
-
*/
|
|
89
28
|
list() {
|
|
90
29
|
return Object.keys(classes);
|
|
91
30
|
},
|
|
92
|
-
/**
|
|
93
|
-
* Get a registered class by name.
|
|
94
|
-
*
|
|
95
|
-
* @param {string} name - The name of the class to retrieve
|
|
96
|
-
* @returns {RegistryConstructor<T>} - The class constructor
|
|
97
|
-
* @throws {Error} If the class is not registered
|
|
98
|
-
*/
|
|
99
31
|
get(name) {
|
|
100
32
|
if (!(name in classes))
|
|
101
33
|
throw new Error(`Class <${name}> not registered for <${reg}>`);
|
|
102
34
|
return classes[name];
|
|
103
35
|
}
|
|
104
36
|
};
|
|
105
|
-
// Register the service in the global registry
|
|
106
37
|
registry[reg] = service;
|
|
107
|
-
// Create a factory function for creating instances from the registry
|
|
108
38
|
factory[reg] = (cls, ...args) => createFromRegistry(reg, cls, ...args);
|
|
109
|
-
// Return the service object
|
|
110
39
|
return service;
|
|
111
40
|
}
|
|
112
|
-
/**
|
|
113
|
-
* Resolve a class constructor from a specific registry.
|
|
114
|
-
*
|
|
115
|
-
* @param {string} reg - The name of the registry
|
|
116
|
-
* @param {T|string} cls - The class itself or name of the class to resolve
|
|
117
|
-
* @returns {T|undefined} - The class constructor if found, otherwise undefined
|
|
118
|
-
* @throws {ReferenceError} If the registry does not exist
|
|
119
|
-
*/
|
|
120
41
|
function resolveCls(reg, cls) {
|
|
121
42
|
if (!(reg in registry))
|
|
122
43
|
throw new ReferenceError(`Registry <${reg}> does not exist`);
|
|
123
44
|
return typeof cls === 'string' ? registry[reg]?.get(cls) : cls;
|
|
124
45
|
}
|
|
125
|
-
/**
|
|
126
|
-
* Create an instance of a class from a specific registry.
|
|
127
|
-
*
|
|
128
|
-
* @param {string} reg - The name of the registry
|
|
129
|
-
* @param {T|string} cls - The class itself or name of the class to instantiate
|
|
130
|
-
* @param {...any} args - Arguments to pass to the class constructor
|
|
131
|
-
* @returns {T} - An instance of the class
|
|
132
|
-
* @throws {Error} If the class cannot be instantiated
|
|
133
|
-
*/
|
|
134
46
|
function createFromRegistry(reg, cls, ...args) {
|
|
135
47
|
cls = resolveCls(reg, cls);
|
|
136
48
|
try {
|