Skip to content

Commit 62b6a64

Browse files
authoredJun 4, 2021
perf: Serialize error stack only when needed (#1359)
* Optimize error stack extraction * Add benchmark
1 parent d4a55b5 commit 62b6a64

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed
 

‎benchmarks/errorStack.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { cronometro } from "cronometro";
2+
import Redis from "../lib/redis";
3+
4+
let redis;
5+
6+
cronometro(
7+
{
8+
default: {
9+
test() {
10+
return redis.set("foo", "bar");
11+
},
12+
before(cb) {
13+
redis = new Redis();
14+
cb();
15+
},
16+
after(cb) {
17+
redis.quit();
18+
cb();
19+
},
20+
},
21+
"showFriendlyErrorStack=true": {
22+
test() {
23+
return redis.set("foo", "bar");
24+
},
25+
before(cb) {
26+
redis = new Redis({ showFriendlyErrorStack: true });
27+
cb();
28+
},
29+
after(cb) {
30+
redis.quit();
31+
cb();
32+
},
33+
},
34+
},
35+
{
36+
print: { compare: true },
37+
}
38+
);

‎lib/command.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface ICommandOptions {
2020
* @memberof ICommandOptions
2121
*/
2222
replyEncoding?: string | null;
23-
errorStack?: string;
23+
errorStack?: Error;
2424
keyPrefix?: string;
2525
/**
2626
* Force the command to be readOnly so it will also execute on slaves
@@ -149,7 +149,7 @@ export default class Command implements ICommand {
149149
public isReadOnly?: boolean;
150150

151151
private replyEncoding: string | null;
152-
private errorStack: string;
152+
private errorStack: Error;
153153
public args: CommandParameter[];
154154
private callback: CallbackFunction;
155155
private transformed = false;
@@ -215,7 +215,7 @@ export default class Command implements ICommand {
215215
this.resolve = this._convertValue(resolve);
216216
if (this.errorStack) {
217217
this.reject = (err) => {
218-
reject(optimizeErrorStack(err, this.errorStack, __dirname));
218+
reject(optimizeErrorStack(err, this.errorStack.stack, __dirname));
219219
};
220220
} else {
221221
this.reject = reject;

‎lib/commander.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ function generateFunction(
160160
}
161161

162162
const options = {
163-
errorStack: this.options.showFriendlyErrorStack
164-
? new Error().stack
165-
: undefined,
163+
errorStack: this.options.showFriendlyErrorStack ? new Error() : undefined,
166164
keyPrefix: this.options.keyPrefix,
167165
replyEncoding: _encoding,
168166
};
@@ -226,7 +224,7 @@ function generateScriptingFunction(
226224
}
227225

228226
if (this.options.showFriendlyErrorStack) {
229-
options.errorStack = new Error().stack;
227+
options.errorStack = new Error();
230228
}
231229

232230
// No auto pipeline, use regular command sending

0 commit comments

Comments
 (0)
Please sign in to comment.