Skip to content

Commit b44e6c6

Browse files
fix: use newer API
1 parent 45b76ba commit b44e6c6

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

website/guide/api-usage/performance-tip.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ There are a lot of tricks to improve performance when using `napi`. The mantra i
99
`parseAsync` can take advantage of NodeJs' libuv thread pool to parse code in parallel threads. This can be faster than the sync version `parse` when handling a lot of code.
1010

1111
```ts
12-
import { js } from '@ast-grep/napi';
12+
import { Lang, parse, parseAsync } from '@ast-grep/napi'
1313
// only one thread parsing
14-
const root = js.parse('console.log("hello world")')
14+
const ast = parse(Lang.JavaScript, 'console.log("hello world")')
15+
const root = ast.root()
1516
// better, can use multiple threads
16-
const root = await js.parseAsync('console.log("hello world")')
17+
const ast2 = await parseAsync(Lang.JavaScript, 'console.log("hello world")')
18+
const root2 = ast2.root()
1719
```
1820

1921
This is especially useful when you are using ast-grep in bundlers where the main thread is busy with other CPU intensive tasks.
@@ -55,7 +57,7 @@ const nodes = root.findAll({kind: 'member_expression'})
5557

5658
## Prefer `findInFiles` when possible
5759

58-
If you have a lot of files to parse and want to maximize your programs' performance, ast-grep's language object provides a `findInFiles` function that parses multiple files and searches relevant nodes in parallel Rust threads.
60+
If you have a lot of files to parse and want to maximize your programs' performance, ast-grep provides a `findInFiles` function that parses multiple files and searches relevant nodes in parallel Rust threads.
5961

6062
APIs we showed above all require parsing code in Rust and pass the `SgRoot` back to JavaScript.
6163
This incurs foreign function communication overhead and only utilizes the single main JavaScript thread.
@@ -66,6 +68,8 @@ The function signature of `findInFiles` is as follows:
6668

6769
```ts
6870
export function findInFiles(
71+
/** the language to parse */
72+
lang: Lang,
6973
/** specify the file path and matcher */
7074
config: FindConfig,
7175
/** callback function for found nodes in a file */
@@ -137,14 +141,19 @@ function countedPromise<F extends Callback>(func: F) {
137141
Example of using `findInFiles`
138142

139143
```ts
140-
let fileCount = await js.findInFiles({
144+
import { Lang, findInFiles } from '@ast-grep/napi'
145+
146+
let fileCount = await findInFiles(Lang.JavaScript, {
141147
paths: ['relative/path/to/code'],
142148
matcher: {
143149
rule: {kind: 'member_expression'}
144150
},
145151
}, (err, n) => {
146-
t.is(err, null)
147-
t.assert(n.length > 0)
148-
t.assert(n[0].text().includes('.'))
152+
if (err) {
153+
console.error(err)
154+
return
155+
}
156+
console.log(`Found ${n.length} nodes`)
157+
console.log(n[0].text())
149158
})
150159
```

0 commit comments

Comments
 (0)