Skip to content

Commit 9b3165f

Browse files
committed
docs(napi/parser): clarify when to use parseAsync vs parseSync (#18486)
## Summary - Add detailed JSDoc documentation to `parse` and `parseSync` functions explaining their performance characteristics - Clarify that `parseSync` is generally preferable since AST deserialization happens on the main thread anyway - Recommend using worker threads with `parseSync` for parallelizing multiple files Closes #15361 🤖 Generated with [Claude Code](https://claude.ai/code)
1 parent dcc4be8 commit 9b3165f

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

napi/parser/src-js/index.d.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,17 @@ export declare const enum ImportNameKind {
142142
}
143143

144144
/**
145-
* Parse asynchronously.
145+
* Parse JS/TS source asynchronously on a separate thread.
146146
*
147-
* Note: This function can be slower than `parseSync` due to the overhead of spawning a thread.
147+
* Note that not all of the workload can happen on a separate thread.
148+
* Parsing on Rust side does happen in a separate thread, but deserialization of the AST to JS objects
149+
* has to happen on current thread. This synchronous deserialization work typically outweighs
150+
* the asynchronous parsing by a factor of between 3 and 20.
151+
*
152+
* i.e. the majority of the workload cannot be parallelized by using this method.
153+
*
154+
* Generally `parseSync` is preferable to use as it does not have the overhead of spawning a thread.
155+
* If you need to parallelize parsing multiple files, it is recommended to use worker threads.
148156
*/
149157
export declare function parse(filename: string, sourceText: string, options?: ParserOptions | undefined | null): Promise<ParseResult>
150158

@@ -189,7 +197,16 @@ export interface ParserOptions {
189197
showSemanticErrors?: boolean
190198
}
191199

192-
/** Parse synchronously. */
200+
/**
201+
* Parse JS/TS source synchronously on current thread.
202+
*
203+
* This is generally preferable over `parse` (async) as it does not have the overhead
204+
* of spawning a thread, and the majority of the workload cannot be parallelized anyway
205+
* (see `parse` documentation for details).
206+
*
207+
* If you need to parallelize parsing multiple files, it is recommended to use worker threads
208+
* with `parseSync` rather than using `parse`.
209+
*/
193210
export declare function parseSync(filename: string, sourceText: string, options?: ParserOptions | undefined | null): ParseResult
194211

195212
/** Returns `true` if raw transfer is supported on this platform. */

napi/parser/src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,14 @@ fn parse_with_return(filename: &str, source_text: &str, options: &ParserOptions)
135135
ParseResult { program_and_fixes, module, comments, errors }
136136
}
137137

138-
/// Parse synchronously.
138+
/// Parse JS/TS source synchronously on current thread.
139+
///
140+
/// This is generally preferable over `parse` (async) as it does not have the overhead
141+
/// of spawning a thread, and the majority of the workload cannot be parallelized anyway
142+
/// (see `parse` documentation for details).
143+
///
144+
/// If you need to parallelize parsing multiple files, it is recommended to use worker threads
145+
/// with `parseSync` rather than using `parse`.
139146
#[napi]
140147
#[allow(clippy::needless_pass_by_value, clippy::allow_attributes)]
141148
pub fn parse_sync(
@@ -168,9 +175,17 @@ impl Task for ResolveTask {
168175
}
169176
}
170177

171-
/// Parse asynchronously.
178+
/// Parse JS/TS source asynchronously on a separate thread.
179+
///
180+
/// Note that not all of the workload can happen on a separate thread.
181+
/// Parsing on Rust side does happen in a separate thread, but deserialization of the AST to JS objects
182+
/// has to happen on current thread. This synchronous deserialization work typically outweighs
183+
/// the asynchronous parsing by a factor of between 3 and 20.
184+
///
185+
/// i.e. the majority of the workload cannot be parallelized by using this method.
172186
///
173-
/// Note: This function can be slower than `parseSync` due to the overhead of spawning a thread.
187+
/// Generally `parseSync` is preferable to use as it does not have the overhead of spawning a thread.
188+
/// If you need to parallelize parsing multiple files, it is recommended to use worker threads.
174189
#[napi]
175190
pub fn parse(
176191
filename: String,

0 commit comments

Comments
 (0)