@asamuzakjp/dom-selector 7.0.6 → 7.0.8
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/package.json +2 -3
- package/src/index.js +58 -27
- package/types/index.d.ts +11 -0
package/package.json
CHANGED
|
@@ -27,8 +27,7 @@
|
|
|
27
27
|
"@asamuzakjp/nwsapi": "^2.3.9",
|
|
28
28
|
"bidi-js": "^1.0.3",
|
|
29
29
|
"css-tree": "^3.2.1",
|
|
30
|
-
"is-potential-custom-element-name": "^1.0.1"
|
|
31
|
-
"lru-cache": "^11.2.7"
|
|
30
|
+
"is-potential-custom-element-name": "^1.0.1"
|
|
32
31
|
},
|
|
33
32
|
"devDependencies": {
|
|
34
33
|
"@types/css-tree": "^2.3.11",
|
|
@@ -72,5 +71,5 @@
|
|
|
72
71
|
"engines": {
|
|
73
72
|
"node": "^20.19.0 || ^22.12.0 || >=24.0.0"
|
|
74
73
|
},
|
|
75
|
-
"version": "7.0.
|
|
74
|
+
"version": "7.0.8"
|
|
76
75
|
}
|
package/src/index.js
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
/* import */
|
|
9
|
-
import { LRUCache } from 'lru-cache';
|
|
10
9
|
import { Finder } from './js/finder.js';
|
|
11
10
|
import { filterSelector, getType, initNwsapi } from './js/utility.js';
|
|
12
11
|
|
|
@@ -22,6 +21,50 @@ import {
|
|
|
22
21
|
} from './js/constant.js';
|
|
23
22
|
const MAX_CACHE = 1024;
|
|
24
23
|
|
|
24
|
+
/* Generational Cache */
|
|
25
|
+
export class GenerationalCache {
|
|
26
|
+
constructor(max) {
|
|
27
|
+
this.max = Math.ceil(max / 2);
|
|
28
|
+
this.current = new Map();
|
|
29
|
+
this.old = new Map();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get(key) {
|
|
33
|
+
let value = this.current.get(key);
|
|
34
|
+
if (value !== undefined) {
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
value = this.old.get(key);
|
|
38
|
+
if (value !== undefined) {
|
|
39
|
+
this.set(key, value);
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
set(key, value) {
|
|
46
|
+
this.current.set(key, value);
|
|
47
|
+
if (this.current.size >= this.max) {
|
|
48
|
+
this.old = this.current;
|
|
49
|
+
this.current = new Map();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
has(key) {
|
|
54
|
+
return this.current.has(key) || this.old.has(key);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
delete(key) {
|
|
58
|
+
this.current.delete(key);
|
|
59
|
+
this.old.delete(key);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
clear() {
|
|
63
|
+
this.current.clear();
|
|
64
|
+
this.old.clear();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
25
68
|
/**
|
|
26
69
|
* @typedef {object} CheckResult
|
|
27
70
|
* @property {boolean} match - The match result.
|
|
@@ -52,9 +95,7 @@ export class DOMSelector {
|
|
|
52
95
|
this.#finder = new Finder(window);
|
|
53
96
|
this.#idlUtils = idlUtils;
|
|
54
97
|
this.#nwsapi = initNwsapi(window, document);
|
|
55
|
-
this.#cache = new
|
|
56
|
-
max: MAX_CACHE
|
|
57
|
-
});
|
|
98
|
+
this.#cache = new GenerationalCache(MAX_CACHE);
|
|
58
99
|
}
|
|
59
100
|
|
|
60
101
|
/**
|
|
@@ -63,6 +104,7 @@ export class DOMSelector {
|
|
|
63
104
|
*/
|
|
64
105
|
clear = () => {
|
|
65
106
|
this.#finder.clearResults(true);
|
|
107
|
+
// this.#cache.clear();
|
|
66
108
|
};
|
|
67
109
|
|
|
68
110
|
/**
|
|
@@ -88,10 +130,8 @@ export class DOMSelector {
|
|
|
88
130
|
node.parentNode
|
|
89
131
|
) {
|
|
90
132
|
const cacheKey = `check_${selector}`;
|
|
91
|
-
let filterMatches =
|
|
92
|
-
if (
|
|
93
|
-
filterMatches = this.#cache.get(cacheKey);
|
|
94
|
-
} else {
|
|
133
|
+
let filterMatches = this.#cache.get(cacheKey);
|
|
134
|
+
if (filterMatches === undefined) {
|
|
95
135
|
filterMatches = filterSelector(selector, TARGET_SELF);
|
|
96
136
|
this.#cache.set(cacheKey, filterMatches);
|
|
97
137
|
}
|
|
@@ -102,9 +142,8 @@ export class DOMSelector {
|
|
|
102
142
|
let ast = null;
|
|
103
143
|
if (match) {
|
|
104
144
|
const astCacheKey = `check_ast_${selector}`;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
} else {
|
|
145
|
+
ast = this.#cache.get(astCacheKey);
|
|
146
|
+
if (ast === undefined) {
|
|
108
147
|
ast = this.#finder.getAST(selector);
|
|
109
148
|
this.#cache.set(astCacheKey, ast);
|
|
110
149
|
}
|
|
@@ -151,10 +190,8 @@ export class DOMSelector {
|
|
|
151
190
|
node.parentNode
|
|
152
191
|
) {
|
|
153
192
|
const cacheKey = `matches_${selector}`;
|
|
154
|
-
let filterMatches =
|
|
155
|
-
if (
|
|
156
|
-
filterMatches = this.#cache.get(cacheKey);
|
|
157
|
-
} else {
|
|
193
|
+
let filterMatches = this.#cache.get(cacheKey);
|
|
194
|
+
if (filterMatches === undefined) {
|
|
158
195
|
filterMatches = filterSelector(selector, TARGET_SELF);
|
|
159
196
|
this.#cache.set(cacheKey, filterMatches);
|
|
160
197
|
}
|
|
@@ -203,10 +240,8 @@ export class DOMSelector {
|
|
|
203
240
|
node.parentNode
|
|
204
241
|
) {
|
|
205
242
|
const cacheKey = `closest_${selector}`;
|
|
206
|
-
let filterMatches =
|
|
207
|
-
if (
|
|
208
|
-
filterMatches = this.#cache.get(cacheKey);
|
|
209
|
-
} else {
|
|
243
|
+
let filterMatches = this.#cache.get(cacheKey);
|
|
244
|
+
if (filterMatches === undefined) {
|
|
210
245
|
filterMatches = filterSelector(selector, TARGET_LINEAL);
|
|
211
246
|
this.#cache.set(cacheKey, filterMatches);
|
|
212
247
|
}
|
|
@@ -262,10 +297,8 @@ export class DOMSelector {
|
|
|
262
297
|
(node.nodeType !== DOCUMENT_FRAGMENT_NODE || !node.host)
|
|
263
298
|
) {
|
|
264
299
|
const cacheKey = `querySelector_${selector}`;
|
|
265
|
-
let filterMatches =
|
|
266
|
-
if (
|
|
267
|
-
filterMatches = this.#cache.get(cacheKey);
|
|
268
|
-
} else {
|
|
300
|
+
let filterMatches = this.#cache.get(cacheKey);
|
|
301
|
+
if (filterMatches === undefined) {
|
|
269
302
|
filterMatches = filterSelector(selector, TARGET_FIRST);
|
|
270
303
|
this.#cache.set(cacheKey, filterMatches);
|
|
271
304
|
}
|
|
@@ -315,10 +348,8 @@ export class DOMSelector {
|
|
|
315
348
|
(node.nodeType !== DOCUMENT_FRAGMENT_NODE || !node.host)
|
|
316
349
|
) {
|
|
317
350
|
const cacheKey = `querySelectorAll_${selector}`;
|
|
318
|
-
let filterMatches =
|
|
319
|
-
if (
|
|
320
|
-
filterMatches = this.#cache.get(cacheKey);
|
|
321
|
-
} else {
|
|
351
|
+
let filterMatches = this.#cache.get(cacheKey);
|
|
352
|
+
if (filterMatches === undefined) {
|
|
322
353
|
filterMatches = filterSelector(selector, TARGET_ALL);
|
|
323
354
|
this.#cache.set(cacheKey, filterMatches);
|
|
324
355
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
export class GenerationalCache {
|
|
2
|
+
constructor(max: any);
|
|
3
|
+
max: number;
|
|
4
|
+
current: Map<any, any>;
|
|
5
|
+
old: Map<any, any>;
|
|
6
|
+
get(key: any): any;
|
|
7
|
+
set(key: any, value: any): void;
|
|
8
|
+
has(key: any): boolean;
|
|
9
|
+
delete(key: any): void;
|
|
10
|
+
clear(): void;
|
|
11
|
+
}
|
|
1
12
|
export class DOMSelector {
|
|
2
13
|
constructor(window: Window, document: Document, opt?: object);
|
|
3
14
|
clear: () => void;
|