Skip to content

Commit

Permalink
perf: Serialize error stack only when needed (#1359)
Browse files Browse the repository at this point in the history
* Optimize error stack extraction

* Add benchmark
  • Loading branch information
aivopaas committed Jun 4, 2021
1 parent d4a55b5 commit 62b6a64
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
38 changes: 38 additions & 0 deletions benchmarks/errorStack.ts
@@ -0,0 +1,38 @@
import { cronometro } from "cronometro";
import Redis from "../lib/redis";

let redis;

cronometro(
{
default: {
test() {
return redis.set("foo", "bar");
},
before(cb) {
redis = new Redis();
cb();
},
after(cb) {
redis.quit();
cb();
},
},
"showFriendlyErrorStack=true": {
test() {
return redis.set("foo", "bar");
},
before(cb) {
redis = new Redis({ showFriendlyErrorStack: true });
cb();
},
after(cb) {
redis.quit();
cb();
},
},
},
{
print: { compare: true },
}
);
6 changes: 3 additions & 3 deletions lib/command.ts
Expand Up @@ -20,7 +20,7 @@ interface ICommandOptions {
* @memberof ICommandOptions
*/
replyEncoding?: string | null;
errorStack?: string;
errorStack?: Error;
keyPrefix?: string;
/**
* Force the command to be readOnly so it will also execute on slaves
Expand Down Expand Up @@ -149,7 +149,7 @@ export default class Command implements ICommand {
public isReadOnly?: boolean;

private replyEncoding: string | null;
private errorStack: string;
private errorStack: Error;
public args: CommandParameter[];
private callback: CallbackFunction;
private transformed = false;
Expand Down Expand Up @@ -215,7 +215,7 @@ export default class Command implements ICommand {
this.resolve = this._convertValue(resolve);
if (this.errorStack) {
this.reject = (err) => {
reject(optimizeErrorStack(err, this.errorStack, __dirname));
reject(optimizeErrorStack(err, this.errorStack.stack, __dirname));
};
} else {
this.reject = reject;
Expand Down
6 changes: 2 additions & 4 deletions lib/commander.ts
Expand Up @@ -160,9 +160,7 @@ function generateFunction(
}

const options = {
errorStack: this.options.showFriendlyErrorStack
? new Error().stack
: undefined,
errorStack: this.options.showFriendlyErrorStack ? new Error() : undefined,
keyPrefix: this.options.keyPrefix,
replyEncoding: _encoding,
};
Expand Down Expand Up @@ -226,7 +224,7 @@ function generateScriptingFunction(
}

if (this.options.showFriendlyErrorStack) {
options.errorStack = new Error().stack;
options.errorStack = new Error();
}

// No auto pipeline, use regular command sending
Expand Down

0 comments on commit 62b6a64

Please sign in to comment.