cmpstr 3.0.2 → 3.0.4
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/dist/CmpStr.esm.js +2228 -4930
- package/dist/CmpStr.esm.js.map +1 -1
- package/dist/CmpStr.esm.min.js +2 -2
- package/dist/CmpStr.esm.min.js.map +1 -1
- package/dist/CmpStr.umd.js +2348 -5026
- package/dist/CmpStr.umd.js.map +1 -1
- package/dist/CmpStr.umd.min.js +2 -2
- package/dist/CmpStr.umd.min.js.map +1 -1
- package/dist/cjs/CmpStr.cjs +10 -404
- package/dist/cjs/CmpStr.cjs.map +1 -1
- package/dist/cjs/CmpStrAsync.cjs +10 -220
- 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 +2 -262
- 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 +37 -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 +1 -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 +2 -90
- package/dist/cjs/utils/Registry.cjs.map +1 -1
- package/dist/cjs/utils/TextAnalyzer.cjs +1 -180
- package/dist/cjs/utils/TextAnalyzer.cjs.map +1 -1
- package/dist/esm/CmpStr.mjs +10 -404
- package/dist/esm/CmpStr.mjs.map +1 -1
- package/dist/esm/CmpStrAsync.mjs +10 -220
- 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 +2 -262
- 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 +29 -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 +1 -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 +2 -90
- package/dist/esm/utils/Registry.mjs.map +1 -1
- package/dist/esm/utils/TextAnalyzer.mjs +1 -180
- package/dist/esm/utils/TextAnalyzer.mjs.map +1 -1
- package/dist/types/index.d.ts +3 -2
- package/dist/types/root.d.ts +38 -0
- package/dist/types/utils/Types.d.ts +1 -0
- package/package.json +15 -9
|
@@ -1,137 +1,46 @@
|
|
|
1
|
-
// CmpStr v3.0.
|
|
1
|
+
// CmpStr v3.0.4 build-74e65a5-250915 by Paul Köhler @komed3 / MIT License
|
|
2
2
|
import { HashTable } from './HashTable.mjs';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Normalizer Utility
|
|
6
|
-
* src/utils/Normalizer.ts
|
|
7
|
-
*
|
|
8
|
-
* @see https://en.wikipedia.org/wiki/Text_normalization
|
|
9
|
-
* @see https://en.wikipedia.org/wiki/Unicode_equivalence
|
|
10
|
-
*
|
|
11
|
-
* This module provides a Normalizer class that allows for string normalization based
|
|
12
|
-
* on various flags. It uses a pipeline of normalization functions that can be reused
|
|
13
|
-
* and cached for efficiency. The Normalizer can handle both single strings and arrays
|
|
14
|
-
* of strings, and supports synchronous and asynchronous normalization.
|
|
15
|
-
*
|
|
16
|
-
* Supported flags:
|
|
17
|
-
* 'd' :: Normalize to NFD (Normalization Form Decomposed)
|
|
18
|
-
* 'u' :: Normalize to NFC (Normalization Form Composed)
|
|
19
|
-
* 'x' :: Normalize to NFKC (Normalization Form Compatibility Composed)
|
|
20
|
-
* 'w' :: Collapse whitespace
|
|
21
|
-
* 't' :: Remove leading and trailing whitespace
|
|
22
|
-
* 'r' :: Remove double characters
|
|
23
|
-
* 's' :: Remove punctuation / special characters
|
|
24
|
-
* 'k' :: Remove non-letter characters
|
|
25
|
-
* 'n' :: Remove non-number characters
|
|
26
|
-
* 'i' :: Case insensitive (convert to lowercase)
|
|
27
|
-
*
|
|
28
|
-
* @module Utils/Normalizer
|
|
29
|
-
* @author Paul Köhler (komed3)
|
|
30
|
-
* @license MIT
|
|
31
|
-
*/
|
|
32
|
-
/**
|
|
33
|
-
* The Normalizer class providing methods to normalize strings based on various flags.
|
|
34
|
-
*/
|
|
35
4
|
class Normalizer {
|
|
36
|
-
/**
|
|
37
|
-
* A map that holds normalization functions based on the flags.
|
|
38
|
-
* This allows for reusing normalization logic without recomputing it.
|
|
39
|
-
*/
|
|
40
5
|
static pipeline = new Map();
|
|
41
|
-
/**
|
|
42
|
-
* A cache to store normalized strings based on the flags and input.
|
|
43
|
-
* This helps avoid recomputing normalization for the same input and flags.
|
|
44
|
-
*/
|
|
45
6
|
static cache = new HashTable();
|
|
46
|
-
/**
|
|
47
|
-
* Returns a normalization function based on the provided flags.
|
|
48
|
-
* The flags are a string of characters that define the normalization steps.
|
|
49
|
-
*
|
|
50
|
-
* @param {NormalizeFlags} flags - A string of characters representing the normalization steps
|
|
51
|
-
* @returns {NormalizerFn} - A function that normalizes a string based on the provided flags
|
|
52
|
-
*/
|
|
53
7
|
static getPipeline(flags) {
|
|
54
|
-
// Return the cached pipeline if it exists
|
|
55
8
|
if (Normalizer.pipeline.has(flags)) return Normalizer.pipeline.get(flags);
|
|
56
|
-
// Define the normalization steps based on the flags
|
|
57
9
|
const steps = [];
|
|
58
|
-
// Normalize to NFD (Normalization Form Decomposed)
|
|
59
10
|
if (flags.includes('d')) steps.push((str) => str.normalize('NFD'));
|
|
60
|
-
// Normalize to NFC (Normalization Form Composed)
|
|
61
11
|
if (flags.includes('u')) steps.push((str) => str.normalize('NFC'));
|
|
62
|
-
// Normalize to NFKC (Normalization Form Compatibility Composed)
|
|
63
12
|
if (flags.includes('x')) steps.push((str) => str.normalize('NFKC'));
|
|
64
|
-
// Collapse whitespace
|
|
65
13
|
if (flags.includes('w')) steps.push((str) => str.replace(/\s+/g, ' '));
|
|
66
|
-
// Remove leading and trailing whitespace
|
|
67
14
|
if (flags.includes('t')) steps.push((str) => str.trim());
|
|
68
|
-
// Remove double characters
|
|
69
15
|
if (flags.includes('r')) steps.push((str) => str.replace(/(.)\1+/g, '$1'));
|
|
70
|
-
// Remove punctuation / special characters
|
|
71
16
|
if (flags.includes('s'))
|
|
72
17
|
steps.push((str) => str.replace(/[^\p{L}\p{N}\s]/gu, ''));
|
|
73
|
-
// Remove non-letter characters
|
|
74
18
|
if (flags.includes('k')) steps.push((str) => str.replace(/[^\p{L}]/gu, ''));
|
|
75
|
-
// Remove non-number characters
|
|
76
19
|
if (flags.includes('n')) steps.push((str) => str.replace(/\p{N}/gu, ''));
|
|
77
|
-
// Case insensitive
|
|
78
20
|
if (flags.includes('i')) steps.push((str) => str.toLowerCase());
|
|
79
|
-
// Build the normalization function from the steps
|
|
80
21
|
const compiled = (input) => {
|
|
81
22
|
let res = input;
|
|
82
23
|
for (const step of steps) res = step(res);
|
|
83
24
|
return res;
|
|
84
25
|
};
|
|
85
|
-
// Cache the compiled function for the given flags
|
|
86
26
|
Normalizer.pipeline.set(flags, compiled);
|
|
87
|
-
// Return the compiled normalization function
|
|
88
27
|
return compiled;
|
|
89
28
|
}
|
|
90
|
-
/**
|
|
91
|
-
* Normalizes the input string or array of strings based on the provided flags.
|
|
92
|
-
* The flags are a string of characters that define the normalization steps.
|
|
93
|
-
*
|
|
94
|
-
* @param {string|string[]} input - The string or array of strings to normalize
|
|
95
|
-
* @param {NormalizeFlags} flags - A string of characters representing the normalization steps
|
|
96
|
-
* @returns {string|string[]} - The normalized string(s)
|
|
97
|
-
*/
|
|
98
29
|
static normalize(input, flags) {
|
|
99
|
-
// If input is an array, normalize each string in the array
|
|
100
30
|
if (Array.isArray(input))
|
|
101
31
|
return input.map((s) => Normalizer.normalize(s, flags));
|
|
102
|
-
// If input or flags are not provided, return the input as is
|
|
103
32
|
if (!flags || typeof flags !== 'string' || !input) return input;
|
|
104
|
-
// Generate a cache key based on the flags and input
|
|
105
33
|
const key = Normalizer.cache.key(flags, [input]);
|
|
106
|
-
// If the key exists in the cache, return the cached result
|
|
107
34
|
if (key && Normalizer.cache.has(key)) return Normalizer.cache.get(key);
|
|
108
|
-
// Normalize the input using the pipeline for the given flags
|
|
109
35
|
const res = Normalizer.getPipeline(flags)(input);
|
|
110
|
-
// If a key was generated, store the result in the cache
|
|
111
36
|
if (key) Normalizer.cache.set(key, res);
|
|
112
|
-
// Return the normalized result
|
|
113
37
|
return res;
|
|
114
38
|
}
|
|
115
|
-
/**
|
|
116
|
-
* Asynchronously normalizes the input string or array of strings based on the
|
|
117
|
-
* provided flags. This method is useful for handling large inputs or when
|
|
118
|
-
* normalization needs to be done in a non-blocking way.
|
|
119
|
-
*
|
|
120
|
-
* @param {string|string[]} input - The string or array of strings to normalize
|
|
121
|
-
* @param {NormalizeFlags} flags - A string of characters representing the normalization steps
|
|
122
|
-
* @returns {Promise<string|string[]>} - A promise that resolves to the normalized string(s)
|
|
123
|
-
*/
|
|
124
39
|
static async normalizeAsync(input, flags) {
|
|
125
40
|
return await (Array.isArray(input)
|
|
126
|
-
?
|
|
127
|
-
|
|
128
|
-
: // If input is a single string, normalize it asynchronously
|
|
129
|
-
Promise.resolve(Normalizer.normalize(input, flags)));
|
|
41
|
+
? Promise.all(input.map((s) => Normalizer.normalize(s, flags)))
|
|
42
|
+
: Promise.resolve(Normalizer.normalize(input, flags)));
|
|
130
43
|
}
|
|
131
|
-
/**
|
|
132
|
-
* Clears the normalization pipeline and cache.
|
|
133
|
-
* This is useful for resetting the state of the Normalizer.
|
|
134
|
-
*/
|
|
135
44
|
static clear() {
|
|
136
45
|
Normalizer.pipeline.clear();
|
|
137
46
|
Normalizer.cache.clear();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Normalizer.mjs","sources":["../../../src/utils/Normalizer.ts"],"sourcesContent":[null],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"Normalizer.mjs","sources":["../../../src/utils/Normalizer.ts"],"sourcesContent":[null],"names":[],"mappings":";;;MAqCa,UAAU,CAAA;AAMX,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAQ,CAAA,CAAA,CAA8B,CAAA,CAAA,CAAA,CAAI,GAAG,CAAA,CAAG;AAMvD,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,CAAA,CAAA,CAAA,CAAA,CAAK,CAAA,CAAA,CAAsC,CAAA,CAAA,CAAA,CAAI,SAAS,CAAA,CAAG;EASlE,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAW,CAAG,CAAA,CAAA,CAAA,CAAA,CAAqB,CAAA,CAAA;AAG9C,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAK,UAAU,CAAC,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAQ,CAAC,CAAA,CAAA,CAAG,CAAE,KAAK,CAAE,EAAG,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,UAAU,CAAC,CAAA,CAAA,CAAA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AAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAU,CAAC,CAAA,CAAA,CAAA,CAAA,CAAK,CAAC,KAAK,CAAA,CAAE;EAE5B;;;;"}
|
package/dist/esm/utils/Pool.mjs
CHANGED
|
@@ -1,101 +1,37 @@
|
|
|
1
|
-
// CmpStr v3.0.
|
|
2
|
-
/**
|
|
3
|
-
* Pool Utility
|
|
4
|
-
* src/utils/Pool.ts
|
|
5
|
-
*
|
|
6
|
-
* @see https://en.wikipedia.org/wiki/Circular_buffer
|
|
7
|
-
*
|
|
8
|
-
* The Pool class provides a simple and efficient buffer pool for dynamic programming
|
|
9
|
-
* algorithms that require temporary arrays (such as Levenshtein, LCS, etc.).
|
|
10
|
-
* By reusing pre-allocated typed arrays, it reduces memory allocations and garbage
|
|
11
|
-
* collection overhead, especially for repeated or batch computations.
|
|
12
|
-
*
|
|
13
|
-
* It supports different types of buffers (Uint16Array, number[], Set, Map) and allows
|
|
14
|
-
* for acquiring buffers of specific sizes while managing a maximum pool size.
|
|
15
|
-
*
|
|
16
|
-
* @module Utils/Pool
|
|
17
|
-
* @author Paul Köhler (komed3)
|
|
18
|
-
* @license MIT
|
|
19
|
-
*/
|
|
20
|
-
/**
|
|
21
|
-
* RingPool is a circular buffer implementation that manages a pool of buffers.
|
|
22
|
-
*
|
|
23
|
-
* It allows for efficient acquisition and release of buffers, ensuring that
|
|
24
|
-
* buffers are reused without unnecessary allocations.
|
|
25
|
-
*
|
|
26
|
-
* @template T - The type of buffers managed by the pool
|
|
27
|
-
*/
|
|
1
|
+
// CmpStr v3.0.4 build-74e65a5-250915 by Paul Köhler @komed3 / MIT License
|
|
28
2
|
class RingPool {
|
|
29
3
|
maxSize;
|
|
30
|
-
// The buffers in the pool
|
|
31
4
|
buffers = [];
|
|
32
|
-
// The current pointer for acquiring buffers
|
|
33
5
|
pointer = 0;
|
|
34
|
-
/**
|
|
35
|
-
* Creates a new RingPool with a specified maximum size.
|
|
36
|
-
*
|
|
37
|
-
* @param {number} maxSize - The maximum number of buffers that can be stored in the pool
|
|
38
|
-
*/
|
|
39
6
|
constructor(maxSize) {
|
|
40
7
|
this.maxSize = maxSize;
|
|
41
8
|
}
|
|
42
|
-
/**
|
|
43
|
-
* Acquires a buffer of at least the specified minimum size from the pool.
|
|
44
|
-
*
|
|
45
|
-
* @param {number} minSize - The minimum size of the buffer to acquire
|
|
46
|
-
* @param {boolean} allowOversize - Whether to allow buffers larger than minSize
|
|
47
|
-
* @return {PoolBuffer<T>|null} - The acquired buffer or null if no suitable buffer is found
|
|
48
|
-
*/
|
|
49
9
|
acquire(minSize, allowOversize) {
|
|
50
10
|
const len = this.buffers.length;
|
|
51
|
-
// Iterate through the buffers in the pool
|
|
52
11
|
for (let i = 0; i < len; i++) {
|
|
53
12
|
const idx = (this.pointer + i) % len;
|
|
54
13
|
const item = this.buffers[idx];
|
|
55
|
-
// Check if the item size is greater than or equal to the minimum size
|
|
56
14
|
if (item.size >= minSize) {
|
|
57
|
-
// Set the pointer to the next position
|
|
58
15
|
this.pointer = (idx + 1) % len;
|
|
59
|
-
// If the item size is equal to minSize or oversize is allowed, return the item
|
|
60
16
|
return allowOversize || item.size === minSize ? item : null;
|
|
61
17
|
}
|
|
62
18
|
}
|
|
63
|
-
// If no suitable buffer is found, return null
|
|
64
19
|
return null;
|
|
65
20
|
}
|
|
66
|
-
/**
|
|
67
|
-
* Releases a buffer back to the pool.
|
|
68
|
-
* If the pool is full, it replaces the oldest buffer with the new one.
|
|
69
|
-
*
|
|
70
|
-
* @param {PoolBuffer<T>} item - The buffer to release back to the pool
|
|
71
|
-
*/
|
|
72
21
|
release(item) {
|
|
73
22
|
if (this.buffers.length < this.maxSize) {
|
|
74
|
-
// If the pool is not full, simply add the item
|
|
75
23
|
this.buffers.push(item);
|
|
76
24
|
} else {
|
|
77
|
-
// If the pool is full, replace the oldest buffer
|
|
78
25
|
this.buffers[this.pointer] = item;
|
|
79
26
|
this.pointer = (this.pointer + 1) % this.maxSize;
|
|
80
27
|
}
|
|
81
28
|
}
|
|
82
|
-
/**
|
|
83
|
-
* Clears the pool, removing all buffers.
|
|
84
|
-
* This resets the pointer and empties the buffer list.
|
|
85
|
-
*/
|
|
86
29
|
clear() {
|
|
87
30
|
this.buffers = [];
|
|
88
31
|
this.pointer = 0;
|
|
89
32
|
}
|
|
90
33
|
}
|
|
91
|
-
/**
|
|
92
|
-
* The Pool class provides a buffer pool for dynamic programming algorithms.
|
|
93
|
-
*
|
|
94
|
-
* It allows for efficient reuse of buffers (Uint16Array, number[], Set, Map)
|
|
95
|
-
* to reduce memory allocations and garbage collection overhead.
|
|
96
|
-
*/
|
|
97
34
|
class Pool {
|
|
98
|
-
// Pool Types
|
|
99
35
|
static CONFIG = {
|
|
100
36
|
uint16: {
|
|
101
37
|
type: 'uint16',
|
|
@@ -112,20 +48,12 @@ class Pool {
|
|
|
112
48
|
set: { type: 'set', maxSize: 8, maxItemSize: 0, allowOversize: false },
|
|
113
49
|
map: { type: 'map', maxSize: 8, maxItemSize: 0, allowOversize: false }
|
|
114
50
|
};
|
|
115
|
-
// Pool Rings for each type
|
|
116
51
|
static POOLS = {
|
|
117
52
|
uint16: new RingPool(32),
|
|
118
53
|
'number[]': new RingPool(16),
|
|
119
54
|
set: new RingPool(8),
|
|
120
55
|
map: new RingPool(8)
|
|
121
56
|
};
|
|
122
|
-
/**
|
|
123
|
-
* Allocates a new buffer of the specified type and size.
|
|
124
|
-
*
|
|
125
|
-
* @param {PoolType} type - The type of buffer to allocate
|
|
126
|
-
* @param {number} size - The size of the buffer to allocate
|
|
127
|
-
* @return {any} - The newly allocated buffer
|
|
128
|
-
*/
|
|
129
57
|
static allocate(type, size) {
|
|
130
58
|
switch (type) {
|
|
131
59
|
case 'uint16':
|
|
@@ -138,53 +66,21 @@ class Pool {
|
|
|
138
66
|
return new Map();
|
|
139
67
|
}
|
|
140
68
|
}
|
|
141
|
-
/**
|
|
142
|
-
* Acquires a buffer of the specified type and size from the pool.
|
|
143
|
-
* If no suitable buffer is available, it allocates a new one.
|
|
144
|
-
*
|
|
145
|
-
* @param {PoolType} type - The type of buffer to acquire (e.g., 'uint16', 'number[]', 'set', 'map')
|
|
146
|
-
* @param {number} size - The size of the buffer to acquire
|
|
147
|
-
* @return {T} - The acquired buffer of the specified type
|
|
148
|
-
*/
|
|
149
69
|
static acquire(type, size) {
|
|
150
|
-
// Get the configuration for the specified type
|
|
151
70
|
const CONFIG = this.CONFIG[type];
|
|
152
|
-
// If the requested size exceeds the maximum item size, allocate a new buffer
|
|
153
71
|
if (size > CONFIG.maxItemSize) return this.allocate(type, size);
|
|
154
|
-
// Try to acquire a buffer from the pool ring
|
|
155
|
-
// If a suitable buffer is found, return it (subarray for uint16)
|
|
156
72
|
const item = this.POOLS[type].acquire(size, CONFIG.allowOversize);
|
|
157
73
|
if (item) {
|
|
158
|
-
// If the type is 'uint16', return a subarray of the buffer
|
|
159
74
|
return type === 'uint16' ? item.buffer.subarray(0, size) : item.buffer;
|
|
160
75
|
}
|
|
161
|
-
// If no suitable buffer is found, allocate a new one
|
|
162
76
|
return this.allocate(type, size);
|
|
163
77
|
}
|
|
164
|
-
/**
|
|
165
|
-
* Acquires multiple buffers of the specified type and sizes from the pool.
|
|
166
|
-
*
|
|
167
|
-
* @param {PoolType} type - The type of buffers to acquire
|
|
168
|
-
* @param {number[]} sizes - An array of sizes for each buffer to acquire
|
|
169
|
-
* @return {T[]} - An array of acquired buffers of the specified type
|
|
170
|
-
*/
|
|
171
78
|
static acquireMany(type, sizes) {
|
|
172
79
|
return sizes.map((size) => this.acquire(type, size));
|
|
173
80
|
}
|
|
174
|
-
/**
|
|
175
|
-
* Releases a buffer back to the pool.
|
|
176
|
-
* If the size of the buffer is larger than the maximum item size, it will not be released.
|
|
177
|
-
*
|
|
178
|
-
* @param {PoolType} type - The type of buffer to release
|
|
179
|
-
* @param {T} buffer - The buffer to release
|
|
180
|
-
* @param {number} size - The size of the buffer
|
|
181
|
-
*/
|
|
182
81
|
static release(type, buffer, size) {
|
|
183
|
-
// Get the configuration for the specified type
|
|
184
82
|
const CONFIG = this.CONFIG[type];
|
|
185
|
-
// If the size of the buffer is less than or equal to the maximum item size, release it
|
|
186
83
|
if (size <= CONFIG.maxItemSize) {
|
|
187
|
-
// Release the buffer back to the pool ring
|
|
188
84
|
this.POOLS[type].release({ buffer, size });
|
|
189
85
|
}
|
|
190
86
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Pool.mjs","sources":["../../../src/utils/Pool.ts"],"sourcesContent":[null],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"Pool.mjs","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,MAAM,CAAA,CAAA,CAAG,CAAA,CAAA,CAAW,IAAI,CAAC,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,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A,CAAA,CAAA,CAAA,CAAA,CAAM,CAAA,CAAE,CAAA,CAAA,CAAA,CAAI,EAAE,CAAE;IAElD;EAEJ;;;;"}
|
|
@@ -1,218 +1,86 @@
|
|
|
1
|
-
// CmpStr v3.0.
|
|
2
|
-
/**
|
|
3
|
-
* Profiler Utility
|
|
4
|
-
* src/utils/profiler.ts
|
|
5
|
-
*
|
|
6
|
-
* @see https://en.wikipedia.org/wiki/Profiling_(computer_programming)
|
|
7
|
-
*
|
|
8
|
-
* This class provides methods to run synchronous and asynchronous functions, capturing
|
|
9
|
-
* their execution time and memory usage, and storing the results in a set of profiler
|
|
10
|
-
* entries. It supports both Node.js and browser environments, detecting the environment
|
|
11
|
-
* automatically.
|
|
12
|
-
*
|
|
13
|
-
* The class is optimized for minimal overhead and can be used for fine-grained
|
|
14
|
-
* performance profiling.
|
|
15
|
-
*
|
|
16
|
-
* @module Utils/Profiler
|
|
17
|
-
* @author Paul Köhler (komed3)
|
|
18
|
-
* @license MIT
|
|
19
|
-
*/
|
|
20
|
-
/**
|
|
21
|
-
* Profiler class for measuring execution time and memory usage of functions.
|
|
22
|
-
*/
|
|
1
|
+
// CmpStr v3.0.4 build-74e65a5-250915 by Paul Köhler @komed3 / MIT License
|
|
23
2
|
class Profiler {
|
|
24
|
-
// Environment detection
|
|
25
3
|
static ENV;
|
|
26
|
-
// Singleton instance
|
|
27
4
|
static instance;
|
|
28
|
-
// Store for profiler entries
|
|
29
5
|
store = new Set();
|
|
30
|
-
// Total time and memory consumption
|
|
31
6
|
totalTime = 0;
|
|
32
7
|
totalMem = 0;
|
|
33
|
-
// The Profiler active state
|
|
34
8
|
active;
|
|
35
|
-
/**
|
|
36
|
-
* Sets the environment based on the available global objects.
|
|
37
|
-
* Detects if running in Node.js or browser and sets the ENV property accordingly.
|
|
38
|
-
*/
|
|
39
9
|
static detectEnv() {
|
|
40
|
-
// Check for Node.js environment
|
|
41
10
|
if (typeof process !== 'undefined') Profiler.ENV = 'nodejs';
|
|
42
|
-
// Check for browser environment
|
|
43
11
|
else if (typeof performance !== 'undefined') Profiler.ENV = 'browser';
|
|
44
|
-
// If neither, set ENV to unknown
|
|
45
12
|
else Profiler.ENV = 'unknown';
|
|
46
13
|
}
|
|
47
|
-
/**
|
|
48
|
-
* Returns the singleton instance of the Perf class.
|
|
49
|
-
* If the instance does not exist, it creates a new one.
|
|
50
|
-
*
|
|
51
|
-
* @param {boolean} [enable=false] - Optional parameter to enable the profiler upon instantiation
|
|
52
|
-
* @returns {Profiler} - Singleton Profiler instance
|
|
53
|
-
*/
|
|
54
14
|
static getInstance(enable) {
|
|
55
|
-
// Ensure the environment is detected
|
|
56
15
|
if (!Profiler.ENV) Profiler.detectEnv();
|
|
57
|
-
// If instance does not exist, create a new one
|
|
58
16
|
if (!Profiler.instance) Profiler.instance = new Profiler(enable);
|
|
59
|
-
// Return singleton instance
|
|
60
17
|
return Profiler.instance;
|
|
61
18
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Private constructor to enforce singleton pattern.
|
|
64
|
-
* Initializes the store for profiler entries.
|
|
65
|
-
*
|
|
66
|
-
* @param {boolean} [enable=false] - Optional parameter to enable the profiler
|
|
67
|
-
*/
|
|
68
19
|
constructor(enable) {
|
|
69
20
|
this.active = enable ?? false;
|
|
70
21
|
}
|
|
71
|
-
/**
|
|
72
|
-
* Gets the current time based on the environment.
|
|
73
|
-
*
|
|
74
|
-
* Uses process.hrtime.bigint() for Node.js, performance.now() for browsers,
|
|
75
|
-
* and Date.now() as a fallback.
|
|
76
|
-
*
|
|
77
|
-
* @returns {number} - Current time in milliseconds
|
|
78
|
-
*/
|
|
79
22
|
now() {
|
|
80
23
|
switch (Profiler.ENV) {
|
|
81
|
-
// Node.js environment
|
|
82
24
|
case 'nodejs':
|
|
83
25
|
return Number(process.hrtime.bigint()) / 1e6;
|
|
84
|
-
// Browser environment
|
|
85
26
|
case 'browser':
|
|
86
27
|
return performance.now();
|
|
87
|
-
// Fallback
|
|
88
28
|
default:
|
|
89
29
|
return Date.now();
|
|
90
30
|
}
|
|
91
31
|
}
|
|
92
|
-
/**
|
|
93
|
-
* Gets the current memory usage based on the environment.
|
|
94
|
-
*
|
|
95
|
-
* Uses process.memoryUsage().heapUsed for Node.js, performance.memory.usedJSHeapSize
|
|
96
|
-
* for browsers, and returns 0 as a fallback.
|
|
97
|
-
*
|
|
98
|
-
* @returns {number} - Current memory usage in bytes
|
|
99
|
-
*/
|
|
100
32
|
mem() {
|
|
101
33
|
switch (Profiler.ENV) {
|
|
102
|
-
// Node.js environment
|
|
103
34
|
case 'nodejs':
|
|
104
35
|
return process.memoryUsage().heapUsed;
|
|
105
|
-
// Browser environment
|
|
106
36
|
case 'browser':
|
|
107
37
|
return performance.memory?.usedJSHeapSize ?? 0;
|
|
108
|
-
// Fallback
|
|
109
38
|
default:
|
|
110
39
|
return 0;
|
|
111
40
|
}
|
|
112
41
|
}
|
|
113
|
-
/**
|
|
114
|
-
* Enables the profiler.
|
|
115
|
-
* Sets the active state to true, allowing profiling to occur.
|
|
116
|
-
*/
|
|
117
42
|
enable() {
|
|
118
43
|
this.active = true;
|
|
119
44
|
}
|
|
120
|
-
/**
|
|
121
|
-
* Disables the profiler.
|
|
122
|
-
* Sets the active state to false, preventing further profiling.
|
|
123
|
-
*/
|
|
124
45
|
disable() {
|
|
125
46
|
this.active = false;
|
|
126
47
|
}
|
|
127
|
-
/**
|
|
128
|
-
* Resets the profiler by clearing the store, total time and memory consumption.
|
|
129
|
-
* This method is useful for starting a new profiling session.
|
|
130
|
-
*/
|
|
131
48
|
clear() {
|
|
132
49
|
this.store.clear();
|
|
133
50
|
this.totalTime = 0;
|
|
134
51
|
this.totalMem = 0;
|
|
135
52
|
}
|
|
136
|
-
/**
|
|
137
|
-
* Runs a synchronous function and profiles its execution time and memory usage.
|
|
138
|
-
* If the profiler is not active, it simply executes the function without profiling.
|
|
139
|
-
*
|
|
140
|
-
* @param {() => T} fn - Function to be executed and profiled
|
|
141
|
-
* @param {Record<string, any>} meta - Metadata to be associated with the profiling entry
|
|
142
|
-
* @returns {T} - The result of the executed function
|
|
143
|
-
*/
|
|
144
53
|
run(fn, meta = {}) {
|
|
145
|
-
// If the profiler is not active, simply execute the function without profiling
|
|
146
54
|
if (!this.active) return fn();
|
|
147
|
-
// Capture the start time and memory usage
|
|
148
55
|
const startTime = this.now(),
|
|
149
56
|
startMem = this.mem();
|
|
150
|
-
// Execute the function and capture the result
|
|
151
57
|
const res = fn();
|
|
152
|
-
// Calculate the time and memory consumption
|
|
153
58
|
const deltaTime = this.now() - startTime;
|
|
154
59
|
const deltaMem = this.mem() - startMem;
|
|
155
|
-
// Add the profiling entry to the store
|
|
156
60
|
this.store.add({ time: deltaTime, mem: deltaMem, res, meta });
|
|
157
61
|
((this.totalTime += deltaTime), (this.totalMem += deltaMem));
|
|
158
|
-
// Return the result of the function
|
|
159
62
|
return res;
|
|
160
63
|
}
|
|
161
|
-
/**
|
|
162
|
-
* Runs an asynchronous function and profiles its execution time and memory usage.
|
|
163
|
-
* If the profiler is not active, it simply executes the function without profiling.
|
|
164
|
-
*
|
|
165
|
-
* @param {() => Promise<T>} fn - Asynchronous function to be executed and profiled
|
|
166
|
-
* @param {Record<string, any>} meta - Metadata to be associated with the profiling entry
|
|
167
|
-
* @returns {Promise<T>} - A promise that resolves to the result of the executed function
|
|
168
|
-
*/
|
|
169
64
|
async runAsync(fn, meta = {}) {
|
|
170
|
-
// If the profiler is not active, simply execute the function without profiling
|
|
171
65
|
if (!this.active) return await fn();
|
|
172
|
-
// Capture the start time and memory usage
|
|
173
66
|
const startTime = this.now(),
|
|
174
67
|
startMem = this.mem();
|
|
175
|
-
// Execute the asynchronous function and wait for its result
|
|
176
68
|
const res = await fn();
|
|
177
|
-
// Calculate the time and memory consumption
|
|
178
69
|
const deltaTime = this.now() - startTime;
|
|
179
70
|
const deltaMem = this.mem() - startMem;
|
|
180
|
-
// Add the profiling entry to the store
|
|
181
71
|
this.store.add({ time: deltaTime, mem: deltaMem, res, meta });
|
|
182
72
|
((this.totalTime += deltaTime), (this.totalMem += deltaMem));
|
|
183
|
-
// Return the result of the function
|
|
184
73
|
return res;
|
|
185
74
|
}
|
|
186
|
-
/**
|
|
187
|
-
* Retrieves all profiler entries stored in the profiler.
|
|
188
|
-
*
|
|
189
|
-
* @returns {ProfilerEntry<any>[]} - An array of profiler entries
|
|
190
|
-
*/
|
|
191
75
|
getAll() {
|
|
192
76
|
return [...this.store];
|
|
193
77
|
}
|
|
194
|
-
/**
|
|
195
|
-
* Retrieves the last profiler entry stored in the profiler.
|
|
196
|
-
*
|
|
197
|
-
* @returns {ProfilerEntry<any> | undefined} - The last profiler entry or undefined if no entries exist
|
|
198
|
-
*/
|
|
199
78
|
getLast() {
|
|
200
79
|
return this.getAll().pop();
|
|
201
80
|
}
|
|
202
|
-
/**
|
|
203
|
-
* Retrieves the total time and memory consumption recorded by the profiler.
|
|
204
|
-
*
|
|
205
|
-
* @returns {{ time: number, mem: number }} - An object containing total time and memory usage
|
|
206
|
-
*/
|
|
207
81
|
getTotal() {
|
|
208
82
|
return { time: this.totalTime, mem: this.totalMem };
|
|
209
83
|
}
|
|
210
|
-
/**
|
|
211
|
-
* Returns the services provided by the Profiler class.
|
|
212
|
-
* This allows for easy access to the profiler's methods.
|
|
213
|
-
*
|
|
214
|
-
* @returns {ProfilerService<any>} - An object containing methods to control the profiler
|
|
215
|
-
*/
|
|
216
84
|
services = {
|
|
217
85
|
enable: this.enable.bind(this),
|
|
218
86
|
disable: this.disable.bind(this),
|