Skip to content

Commit fa4495f

Browse files
marcbachmannluin
authored andcommitted
chore: Reduce amount of command args iterations
The happy case should be slightly faster that way when there are multiple arguments
1 parent 80b3410 commit fa4495f

File tree

3 files changed

+16
-27
lines changed

3 files changed

+16
-27
lines changed

lib/command.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export default class Command implements Respondable {
154154

155155
private replyEncoding: BufferEncoding | null;
156156
private errorStack: Error;
157+
private bufferMode: boolean;
157158
public args: CommandParameter[];
158159
private callback: CallbackFunction;
159160
private transformed = false;
@@ -270,14 +271,6 @@ export default class Command implements Respondable {
270271
* @public
271272
*/
272273
public toWritable(_socket: object): string | Buffer {
273-
let bufferMode = false;
274-
for (const arg of this.args) {
275-
if (arg instanceof Buffer) {
276-
bufferMode = true;
277-
break;
278-
}
279-
}
280-
281274
let result;
282275
const commandStr =
283276
"*" +
@@ -287,10 +280,12 @@ export default class Command implements Respondable {
287280
"\r\n" +
288281
this.name +
289282
"\r\n";
290-
if (bufferMode) {
283+
284+
if (this.bufferMode) {
291285
const buffers = new MixedBuffers();
292286
buffers.push(commandStr);
293-
for (const arg of this.args) {
287+
for (let i = 0; i < this.args.length; ++i) {
288+
const arg = this.args[i];
294289
if (arg instanceof Buffer) {
295290
if (arg.length === 0) {
296291
buffers.push("$0\r\n\r\n");
@@ -312,7 +307,8 @@ export default class Command implements Respondable {
312307
result = buffers.toBuffer();
313308
} else {
314309
result = commandStr;
315-
for (const arg of this.args) {
310+
for (let i = 0; i < this.args.length; ++i) {
311+
const arg = this.args[i];
316312
result +=
317313
"$" +
318314
Buffer.byteLength(arg as string | Buffer) +
@@ -327,7 +323,11 @@ export default class Command implements Respondable {
327323
stringifyArguments(): void {
328324
for (let i = 0; i < this.args.length; ++i) {
329325
const arg = this.args[i];
330-
if (!(arg instanceof Buffer) && typeof arg !== "string") {
326+
if (typeof arg === 'string') {
327+
// buffers and strings don't need any transformation
328+
} else if (arg instanceof Buffer) {
329+
this.bufferMode = true;
330+
} else {
331331
this.args[i] = toArg(arg);
332332
}
333333
}

lib/script.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default class Script {
5858

5959
const evalsha = new this.Command(
6060
"evalsha",
61-
[this.sha].concat(args),
61+
[this.sha, ...args],
6262
options
6363
);
6464
evalsha.isCustomCommand = true;
@@ -72,7 +72,7 @@ export default class Script {
7272
// in case it's not loaded yet on the connectionDo an eval as fallback, redis will hash and load it
7373
const resend = new this.Command(
7474
"evalsha",
75-
[this.sha].concat(args),
75+
[this.sha, ...args],
7676
options
7777
);
7878
resend.isCustomCommand = true;

lib/utils/Commander.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,8 @@ function generateScriptingFunction(
196196
script: Script,
197197
encoding: unknown
198198
) {
199-
return function () {
200-
let length = arguments.length;
201-
const lastArgIndex = length - 1;
202-
let callback = arguments[lastArgIndex];
203-
if (typeof callback !== "function") {
204-
callback = undefined;
205-
} else {
206-
length = lastArgIndex;
207-
}
208-
const args = new Array(length);
209-
for (let i = 0; i < length; i++) {
210-
args[i] = arguments[i];
211-
}
199+
return function (...args) {
200+
const callback = typeof args[args.length - 1] === "function" ? args.pop() : undefined;
212201

213202
let options;
214203
if (this.options.dropBufferSupport) {

0 commit comments

Comments
 (0)