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,139 +1,48 @@
|
|
|
1
|
-
// CmpStr v3.0.
|
|
1
|
+
// CmpStr v3.0.4 build-74e65a5-250915 by Paul Köhler @komed3 / MIT License
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
var HashTable = require('./HashTable.cjs');
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* Normalizer Utility
|
|
8
|
-
* src/utils/Normalizer.ts
|
|
9
|
-
*
|
|
10
|
-
* @see https://en.wikipedia.org/wiki/Text_normalization
|
|
11
|
-
* @see https://en.wikipedia.org/wiki/Unicode_equivalence
|
|
12
|
-
*
|
|
13
|
-
* This module provides a Normalizer class that allows for string normalization based
|
|
14
|
-
* on various flags. It uses a pipeline of normalization functions that can be reused
|
|
15
|
-
* and cached for efficiency. The Normalizer can handle both single strings and arrays
|
|
16
|
-
* of strings, and supports synchronous and asynchronous normalization.
|
|
17
|
-
*
|
|
18
|
-
* Supported flags:
|
|
19
|
-
* 'd' :: Normalize to NFD (Normalization Form Decomposed)
|
|
20
|
-
* 'u' :: Normalize to NFC (Normalization Form Composed)
|
|
21
|
-
* 'x' :: Normalize to NFKC (Normalization Form Compatibility Composed)
|
|
22
|
-
* 'w' :: Collapse whitespace
|
|
23
|
-
* 't' :: Remove leading and trailing whitespace
|
|
24
|
-
* 'r' :: Remove double characters
|
|
25
|
-
* 's' :: Remove punctuation / special characters
|
|
26
|
-
* 'k' :: Remove non-letter characters
|
|
27
|
-
* 'n' :: Remove non-number characters
|
|
28
|
-
* 'i' :: Case insensitive (convert to lowercase)
|
|
29
|
-
*
|
|
30
|
-
* @module Utils/Normalizer
|
|
31
|
-
* @author Paul Köhler (komed3)
|
|
32
|
-
* @license MIT
|
|
33
|
-
*/
|
|
34
|
-
/**
|
|
35
|
-
* The Normalizer class providing methods to normalize strings based on various flags.
|
|
36
|
-
*/
|
|
37
6
|
class Normalizer {
|
|
38
|
-
/**
|
|
39
|
-
* A map that holds normalization functions based on the flags.
|
|
40
|
-
* This allows for reusing normalization logic without recomputing it.
|
|
41
|
-
*/
|
|
42
7
|
static pipeline = new Map();
|
|
43
|
-
/**
|
|
44
|
-
* A cache to store normalized strings based on the flags and input.
|
|
45
|
-
* This helps avoid recomputing normalization for the same input and flags.
|
|
46
|
-
*/
|
|
47
8
|
static cache = new HashTable.HashTable();
|
|
48
|
-
/**
|
|
49
|
-
* Returns a normalization function based on the provided flags.
|
|
50
|
-
* The flags are a string of characters that define the normalization steps.
|
|
51
|
-
*
|
|
52
|
-
* @param {NormalizeFlags} flags - A string of characters representing the normalization steps
|
|
53
|
-
* @returns {NormalizerFn} - A function that normalizes a string based on the provided flags
|
|
54
|
-
*/
|
|
55
9
|
static getPipeline(flags) {
|
|
56
|
-
// Return the cached pipeline if it exists
|
|
57
10
|
if (Normalizer.pipeline.has(flags)) return Normalizer.pipeline.get(flags);
|
|
58
|
-
// Define the normalization steps based on the flags
|
|
59
11
|
const steps = [];
|
|
60
|
-
// Normalize to NFD (Normalization Form Decomposed)
|
|
61
12
|
if (flags.includes('d')) steps.push((str) => str.normalize('NFD'));
|
|
62
|
-
// Normalize to NFC (Normalization Form Composed)
|
|
63
13
|
if (flags.includes('u')) steps.push((str) => str.normalize('NFC'));
|
|
64
|
-
// Normalize to NFKC (Normalization Form Compatibility Composed)
|
|
65
14
|
if (flags.includes('x')) steps.push((str) => str.normalize('NFKC'));
|
|
66
|
-
// Collapse whitespace
|
|
67
15
|
if (flags.includes('w')) steps.push((str) => str.replace(/\s+/g, ' '));
|
|
68
|
-
// Remove leading and trailing whitespace
|
|
69
16
|
if (flags.includes('t')) steps.push((str) => str.trim());
|
|
70
|
-
// Remove double characters
|
|
71
17
|
if (flags.includes('r')) steps.push((str) => str.replace(/(.)\1+/g, '$1'));
|
|
72
|
-
// Remove punctuation / special characters
|
|
73
18
|
if (flags.includes('s'))
|
|
74
19
|
steps.push((str) => str.replace(/[^\p{L}\p{N}\s]/gu, ''));
|
|
75
|
-
// Remove non-letter characters
|
|
76
20
|
if (flags.includes('k')) steps.push((str) => str.replace(/[^\p{L}]/gu, ''));
|
|
77
|
-
// Remove non-number characters
|
|
78
21
|
if (flags.includes('n')) steps.push((str) => str.replace(/\p{N}/gu, ''));
|
|
79
|
-
// Case insensitive
|
|
80
22
|
if (flags.includes('i')) steps.push((str) => str.toLowerCase());
|
|
81
|
-
// Build the normalization function from the steps
|
|
82
23
|
const compiled = (input) => {
|
|
83
24
|
let res = input;
|
|
84
25
|
for (const step of steps) res = step(res);
|
|
85
26
|
return res;
|
|
86
27
|
};
|
|
87
|
-
// Cache the compiled function for the given flags
|
|
88
28
|
Normalizer.pipeline.set(flags, compiled);
|
|
89
|
-
// Return the compiled normalization function
|
|
90
29
|
return compiled;
|
|
91
30
|
}
|
|
92
|
-
/**
|
|
93
|
-
* Normalizes the input string or array of strings based on the provided flags.
|
|
94
|
-
* The flags are a string of characters that define the normalization steps.
|
|
95
|
-
*
|
|
96
|
-
* @param {string|string[]} input - The string or array of strings to normalize
|
|
97
|
-
* @param {NormalizeFlags} flags - A string of characters representing the normalization steps
|
|
98
|
-
* @returns {string|string[]} - The normalized string(s)
|
|
99
|
-
*/
|
|
100
31
|
static normalize(input, flags) {
|
|
101
|
-
// If input is an array, normalize each string in the array
|
|
102
32
|
if (Array.isArray(input))
|
|
103
33
|
return input.map((s) => Normalizer.normalize(s, flags));
|
|
104
|
-
// If input or flags are not provided, return the input as is
|
|
105
34
|
if (!flags || typeof flags !== 'string' || !input) return input;
|
|
106
|
-
// Generate a cache key based on the flags and input
|
|
107
35
|
const key = Normalizer.cache.key(flags, [input]);
|
|
108
|
-
// If the key exists in the cache, return the cached result
|
|
109
36
|
if (key && Normalizer.cache.has(key)) return Normalizer.cache.get(key);
|
|
110
|
-
// Normalize the input using the pipeline for the given flags
|
|
111
37
|
const res = Normalizer.getPipeline(flags)(input);
|
|
112
|
-
// If a key was generated, store the result in the cache
|
|
113
38
|
if (key) Normalizer.cache.set(key, res);
|
|
114
|
-
// Return the normalized result
|
|
115
39
|
return res;
|
|
116
40
|
}
|
|
117
|
-
/**
|
|
118
|
-
* Asynchronously normalizes the input string or array of strings based on the
|
|
119
|
-
* provided flags. This method is useful for handling large inputs or when
|
|
120
|
-
* normalization needs to be done in a non-blocking way.
|
|
121
|
-
*
|
|
122
|
-
* @param {string|string[]} input - The string or array of strings to normalize
|
|
123
|
-
* @param {NormalizeFlags} flags - A string of characters representing the normalization steps
|
|
124
|
-
* @returns {Promise<string|string[]>} - A promise that resolves to the normalized string(s)
|
|
125
|
-
*/
|
|
126
41
|
static async normalizeAsync(input, flags) {
|
|
127
42
|
return await (Array.isArray(input)
|
|
128
|
-
?
|
|
129
|
-
|
|
130
|
-
: // If input is a single string, normalize it asynchronously
|
|
131
|
-
Promise.resolve(Normalizer.normalize(input, flags)));
|
|
43
|
+
? Promise.all(input.map((s) => Normalizer.normalize(s, flags)))
|
|
44
|
+
: Promise.resolve(Normalizer.normalize(input, flags)));
|
|
132
45
|
}
|
|
133
|
-
/**
|
|
134
|
-
* Clears the normalization pipeline and cache.
|
|
135
|
-
* This is useful for resetting the state of the Normalizer.
|
|
136
|
-
*/
|
|
137
46
|
static clear() {
|
|
138
47
|
Normalizer.pipeline.clear();
|
|
139
48
|
Normalizer.cache.clear();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Normalizer.cjs","sources":["../../../src/utils/Normalizer.ts"],"sourcesContent":[null],"names":["HashTable"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"Normalizer.cjs","sources":["../../../src/utils/Normalizer.ts"],"sourcesContent":[null],"names":["HashTable"],"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,CAAIA,mBAAS,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,CA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CAAC,KAAK,CAAA,CAAE;AAC3B,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAU,CAAC,CAAA,CAAA,CAAA,CAAA,CAAK,CAAC,KAAK,CAAA,CAAE;EAE5B;;;;"}
|
package/dist/cjs/utils/Pool.cjs
CHANGED
|
@@ -1,103 +1,39 @@
|
|
|
1
|
-
// CmpStr v3.0.
|
|
1
|
+
// CmpStr v3.0.4 build-74e65a5-250915 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',
|
|
@@ -114,20 +50,12 @@ class Pool {
|
|
|
114
50
|
set: { type: 'set', maxSize: 8, maxItemSize: 0, allowOversize: false },
|
|
115
51
|
map: { type: 'map', maxSize: 8, maxItemSize: 0, allowOversize: false }
|
|
116
52
|
};
|
|
117
|
-
// Pool Rings for each type
|
|
118
53
|
static POOLS = {
|
|
119
54
|
uint16: new RingPool(32),
|
|
120
55
|
'number[]': new RingPool(16),
|
|
121
56
|
set: new RingPool(8),
|
|
122
57
|
map: new RingPool(8)
|
|
123
58
|
};
|
|
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
59
|
static allocate(type, size) {
|
|
132
60
|
switch (type) {
|
|
133
61
|
case 'uint16':
|
|
@@ -140,53 +68,21 @@ class Pool {
|
|
|
140
68
|
return new Map();
|
|
141
69
|
}
|
|
142
70
|
}
|
|
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
71
|
static acquire(type, size) {
|
|
152
|
-
// Get the configuration for the specified type
|
|
153
72
|
const CONFIG = this.CONFIG[type];
|
|
154
|
-
// If the requested size exceeds the maximum item size, allocate a new buffer
|
|
155
73
|
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
74
|
const item = this.POOLS[type].acquire(size, CONFIG.allowOversize);
|
|
159
75
|
if (item) {
|
|
160
|
-
// If the type is 'uint16', return a subarray of the buffer
|
|
161
76
|
return type === 'uint16' ? item.buffer.subarray(0, size) : item.buffer;
|
|
162
77
|
}
|
|
163
|
-
// If no suitable buffer is found, allocate a new one
|
|
164
78
|
return this.allocate(type, size);
|
|
165
79
|
}
|
|
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
80
|
static acquireMany(type, sizes) {
|
|
174
81
|
return sizes.map((size) => this.acquire(type, size));
|
|
175
82
|
}
|
|
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
83
|
static release(type, buffer, size) {
|
|
185
|
-
// Get the configuration for the specified type
|
|
186
84
|
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
85
|
if (size <= CONFIG.maxItemSize) {
|
|
189
|
-
// Release the buffer back to the pool ring
|
|
190
86
|
this.POOLS[type].release({ buffer, size });
|
|
191
87
|
}
|
|
192
88
|
}
|
|
@@ -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,CA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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.0.4 build-74e65a5-250915 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),
|