Skip to content

Commit 6e3d3d3

Browse files
committed
Improve precaptureStackTraces perf
1 parent 12edc18 commit 6e3d3d3

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ This driver uses semantic versioning:
2424
useful than intended. Now parsing errors, `ArangoError` instances and HTTP
2525
errors also receive improved error stack traces when this option is enabled.
2626

27+
- Improved performance for `precaptureStackTraces` when no errors occur
28+
29+
The generated stack is now only accessed on demand, allowing the runtime to
30+
delay generation of the stack trace string. Previously the stack would always
31+
be accessed prior to the request being sent, causing a noticeable delay even
32+
when no error occurs.
33+
2734
## [7.3.0] - 2021-03-08
2835

2936
### Changed

src/connection.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function generateStackTrace() {
133133
err = e;
134134
}
135135
}
136-
return err.stack;
136+
return err;
137137
}
138138

139139
/**
@@ -286,7 +286,7 @@ export type RequestOptions = {
286286
*/
287287
type Task = {
288288
host?: number;
289-
stack?: string;
289+
stack?: () => string;
290290
allowDirtyRead: boolean;
291291
resolve: Function;
292292
reject: Function;
@@ -614,7 +614,7 @@ export class Connection {
614614
this._queue.push(task);
615615
} else {
616616
if (task.stack) {
617-
err.stack += task.stack;
617+
err.stack += task.stack();
618618
}
619619
task.reject(err);
620620
}
@@ -895,7 +895,7 @@ export class Connection {
895895
}
896896
e.response = res;
897897
if (task.stack) {
898-
e.stack += task.stack;
898+
e.stack += task.stack();
899899
}
900900
reject(e);
901901
return;
@@ -910,14 +910,14 @@ export class Connection {
910910
res.body = parsedBody;
911911
const err = new ArangoError(res);
912912
if (task.stack) {
913-
err.stack += task.stack;
913+
err.stack += task.stack();
914914
}
915915
reject(err);
916916
} else if (res.statusCode && res.statusCode >= 400) {
917917
res.body = parsedBody;
918918
const err = new HttpError(res);
919919
if (task.stack) {
920-
err.stack += task.stack;
920+
err.stack += task.stack();
921921
}
922922
reject(err);
923923
} else {
@@ -929,12 +929,15 @@ export class Connection {
929929

930930
if (this._precaptureStackTraces) {
931931
if (typeof Error.captureStackTrace === "function") {
932-
Error.captureStackTrace(task);
933-
task.stack = `\n${task.stack!.split("\n").slice(3).join("\n")}`;
932+
const capture = {} as { readonly stack: string };
933+
Error.captureStackTrace(capture);
934+
task.stack = () =>
935+
`\n${capture.stack.split("\n").slice(3).join("\n")}`;
934936
} else {
935-
const stack = generateStackTrace();
936-
if (stack) {
937-
task.stack = `\n${stack.split("\n").slice(4).join("\n")}`;
937+
const capture = generateStackTrace() as { readonly stack: string };
938+
if (Object.prototype.hasOwnProperty.call(capture, "stack")) {
939+
task.stack = () =>
940+
`\n${capture.stack.split("\n").slice(4).join("\n")}`;
938941
}
939942
}
940943
}

0 commit comments

Comments
 (0)