gonia 0.1.1 → 0.1.2

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.
Files changed (2) hide show
  1. package/dist/context.js +32 -8
  2. package/package.json +1 -1
package/dist/context.js CHANGED
@@ -4,6 +4,36 @@
4
4
  * @packageDocumentation
5
5
  */
6
6
  import { findRoots } from './expression.js';
7
+ /**
8
+ * LRU cache for compiled expressions.
9
+ * Map maintains insertion order; we delete and re-add on access to maintain LRU order.
10
+ */
11
+ const exprCache = new Map();
12
+ const CACHE_MAX_SIZE = 500;
13
+ /**
14
+ * Get or compile an expression.
15
+ */
16
+ function getOrCompile(expr) {
17
+ let cached = exprCache.get(expr);
18
+ if (cached) {
19
+ // Move to end (most recently used)
20
+ exprCache.delete(expr);
21
+ exprCache.set(expr, cached);
22
+ return cached;
23
+ }
24
+ const roots = findRoots(expr);
25
+ const fn = new Function(...roots, `return (${expr})`);
26
+ cached = { fn, roots };
27
+ // Evict oldest if at capacity
28
+ if (exprCache.size >= CACHE_MAX_SIZE) {
29
+ const oldest = exprCache.keys().next().value;
30
+ if (oldest !== undefined) {
31
+ exprCache.delete(oldest);
32
+ }
33
+ }
34
+ exprCache.set(expr, cached);
35
+ return cached;
36
+ }
7
37
  /**
8
38
  * Create an evaluation context for directives.
9
39
  *
@@ -34,14 +64,8 @@ export function createContext(mode, state, scope = {}) {
34
64
  const ctx = {
35
65
  mode,
36
66
  eval(expr) {
37
- const roots = findRoots(expr);
38
- const fn = new Function(...roots, `return (${expr})`);
39
- const values = roots.map(r => {
40
- if (r in scope) {
41
- return scope[r];
42
- }
43
- return state[r];
44
- });
67
+ const { fn, roots } = getOrCompile(expr);
68
+ const values = roots.map(r => r in scope ? scope[r] : state[r]);
45
69
  return fn(...values);
46
70
  },
47
71
  get(key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gonia",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "A lightweight, SSR-first reactive UI library with declarative directives",
5
5
  "type": "module",
6
6
  "license": "MIT",