From 62b6a648910eccc3d83a3acd2db873704fd2080a Mon Sep 17 00:00:00 2001 From: Aivo Paas Date: Fri, 4 Jun 2021 06:08:30 +0300 Subject: [PATCH] perf: Serialize error stack only when needed (#1359) * Optimize error stack extraction * Add benchmark --- benchmarks/errorStack.ts | 38 ++++++++++++++++++++++++++++++++++++++ lib/command.ts | 6 +++--- lib/commander.ts | 6 ++---- 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 benchmarks/errorStack.ts diff --git a/benchmarks/errorStack.ts b/benchmarks/errorStack.ts new file mode 100644 index 00000000..0af24734 --- /dev/null +++ b/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 }, + } +); diff --git a/lib/command.ts b/lib/command.ts index 8b0c59ab..8fc166fb 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -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 @@ -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; @@ -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; diff --git a/lib/commander.ts b/lib/commander.ts index 4031e830..644ee73a 100644 --- a/lib/commander.ts +++ b/lib/commander.ts @@ -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, }; @@ -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