From 077b7870f7d17b606165c694fb5444e6654e2c6c Mon Sep 17 00:00:00 2001 From: Erlend Oftedal Date: Mon, 5 Feb 2024 16:00:04 +0100 Subject: [PATCH] Speedup --- CHANGELOG.md | 6 ++++++ package.json | 2 +- src/index.ts | 25 +++++++++++++------------ tsconfig.json | 3 ++- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1adbfd..2dde069 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [1.0.0-beta.2] - 2024-02-05 + +### Fixes + +* Performance update + ## [1.0.0-beta.1] - 2024-02-01 Fixing build diff --git a/package.json b/package.json index 2f989fa..debb81a 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/eoftedal/babel-q.git" + "url": "https://github.com/RetireJS/babel-q.git" }, "author": "Erlend Oftedal ", "license": "Apache-2.0", diff --git a/src/index.ts b/src/index.ts index 9462c57..dc00882 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,6 +38,7 @@ type State = { child: FNode[][]; descendant: FNode[][]; filters: FilterResult[][]; + matches: [FNode, NodePath][][]; } type FilterCondition = { type: "and" | "or" | "equals"; @@ -129,6 +130,7 @@ function isMatch(fnode: FNode, path: NodePath) : boolean { function addIfTokenMatch(fnode: FNode, path: NodePath, state: State) { if (!isMatch(fnode, path)) return; + state.matches[state.depth].push([fnode, path]); if (fnode.node.filter) { const filter = createFilter(fnode.node.filter, []); const filteredResult: Array = []; @@ -149,10 +151,9 @@ function isPrimitive(value: unknown) : boolean { } function addPrimitiveAttributeIfMatch(fnode: FNode, path: NodePath) { - if (!fnode.node.attribute) return; - if (!fnode.node.value) return; - if (fnode.node.child) return; - if (fnode.node.filter) return; + if (!fnode.node.attribute || !fnode.node.value) return; + if (fnode.node.child || fnode.node.filter) return; + if (!Object.hasOwn(path.node, fnode.node.value)) return; const lookup = path.get(fnode.node.value); const nodes = (Array.isArray(lookup) ? lookup : [lookup]) .filter(n => n.node != undefined) @@ -245,11 +246,9 @@ function resolveDirectly(node: QNode, path: NodePath) : Result[] { } function addResultIfTokenMatch(fnode: FNode, path: NodePath, state: State) { - if (!isMatch(fnode, path)) return; const filters = state.filters[state.depth].filter(f => f.node == path.node && f.qNode == fnode.node); const matchingFilters = filters.filter(f => evaluateFilter(f.filter, path).length > 0); log.debug("RESULT MATCH", fnode.node.value, breadCrumb(path), filters.length, matchingFilters.length); - if (filters.length > 0) log.debug("HOLA FILTROS", filters, matchingFilters); if (fnode.node.child) { if (fnode.node.binding && (filters.length == 0 || matchingFilters.length > 0)) { const binding = resolveBinding(path); @@ -281,7 +280,8 @@ function travHandle>(queries: T, root: NodePath< depth: 0, child: [[],[]], descendant: [[],[]], - filters: [[],[]] + filters: [[],[]], + matches: [[]] }; Object.entries(queries).forEach(([name, node]) => { createFNodeAndAddToState(node, results[name], state); @@ -296,6 +296,7 @@ function travHandle>(queries: T, root: NodePath< state.child.push([]); state.descendant.push([]); state.filters.push([]); + state.matches.push([]); state.child[state.depth].forEach(fnode => addIfTokenMatch(fnode, path, state)); state.descendant.slice(0, state.depth+1).forEach(fnodes => fnodes.forEach(fnode => addIfTokenMatch(fnode, path, state)) @@ -303,18 +304,18 @@ function travHandle>(queries: T, root: NodePath< }, exit(path, state) { log.debug("EXIT", breadCrumb(path)); + // Check for attributes as not all attributes are visited state.child[state.depth +1].forEach(fnode => addPrimitiveAttributeIfMatch(fnode, path)); - state.descendant.slice(0, state.depth+2).forEach(fnodes => + state.descendant.forEach(fnodes => fnodes.forEach(fnode => addPrimitiveAttributeIfMatch(fnode, path)) ); - state.child[state.depth].forEach(fnode => addResultIfTokenMatch(fnode, path, state)); - state.descendant.slice(0, state.depth+1).forEach(fnodes => - fnodes.forEach(fnode => addResultIfTokenMatch(fnode, path, state)) - ); + + state.matches[state.depth].forEach(([fNode, path]) => addResultIfTokenMatch(fNode, path, state)); state.depth--; state.child.pop(); state.descendant.pop(); state.filters.pop(); + state.matches.pop(); } }, root.scope, state, root); return results; diff --git a/tsconfig.json b/tsconfig.json index 3f7b894..0265ef1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { - "target": "ES2016", + "lib" : [ "ES2022" ], + "target": "ES2022", "module": "commonjs", "declaration": true, "outDir": "./lib",