@asamuzakjp/dom-selector 7.0.6 → 7.0.7

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 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.6"
74
+ "version": "7.0.7"
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 LRUCache({
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
  /**
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;