Skip to content

Commit 637e59f

Browse files
author
Ruben Bridgewater
committed
Improve arguments parsing
1 parent 835dc40 commit 637e59f

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

index.js

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -948,20 +948,17 @@ commands.list.forEach(function (command) {
948948
RedisClient.prototype[command.toUpperCase()] = RedisClient.prototype[command] = function (key, arg, callback) {
949949
if (Array.isArray(key)) {
950950
return this.send_command(command, key, arg);
951-
}
952-
if (Array.isArray(arg)) {
951+
} else if (Array.isArray(arg)) {
953952
arg = [key].concat(arg);
954953
return this.send_command(command, arg, callback);
955954
}
956-
// Speed up the common case
955+
// This has to be inlined, otherwise the arguments leak
957956
var len = arguments.length;
958-
if (len === 2) {
959-
return this.send_command(command, [key, arg]);
960-
}
961-
if (len === 3) {
962-
return this.send_command(command, [key, arg, callback]);
957+
var arr = new Array(len);
958+
for (var i = 0; i < len; i += 1) {
959+
arr[i] = arguments[i];
963960
}
964-
return this.send_command(command, utils.to_array(arguments));
961+
return this.send_command(command, arr);
965962
};
966963

967964
Multi.prototype[command.toUpperCase()] = Multi.prototype[command] = function (key, arg, callback) {
@@ -976,15 +973,12 @@ commands.list.forEach(function (command) {
976973
}
977974
this.queue.push([command, key].concat(arg));
978975
} else {
979-
// Speed up the common case
980976
var len = arguments.length;
981-
if (len === 2) {
982-
this.queue.push([command, key, arg]);
983-
} else if (len === 3) {
984-
this.queue.push([command, key, arg, callback]);
985-
} else {
986-
this.queue.push([command].concat(utils.to_array(arguments)));
977+
var arr = new Array(len);
978+
for (var i = 0; i < len; i += 1) {
979+
arr[i] = arguments[i];
987980
}
981+
this.queue.push([command].concat(arr));
988982
}
989983
return this;
990984
};
@@ -1051,23 +1045,22 @@ RedisClient.prototype.hmset = RedisClient.prototype.HMSET = function (key, args,
10511045
if (Array.isArray(args)) {
10521046
return this.send_command('hmset', [key].concat(args), callback);
10531047
}
1054-
if (typeof args === 'object') {
1048+
if (typeof args === 'object' && (typeof callback === 'function' || typeof callback === 'undefined')) {
10551049
// User does: client.hmset(key, {key1: val1, key2: val2})
10561050
// assuming key is a string, i.e. email address
1057-
1058-
// if key is a number, i.e. timestamp, convert to string
1059-
// TODO: This seems random and no other command get's the key converted => either all or none should behave like this
1060-
if (typeof key !== 'string') {
1061-
key = key.toString();
1062-
}
10631051
tmp_args = [key];
10641052
var fields = Object.keys(args);
10651053
while (field = fields.shift()) {
10661054
tmp_args.push(field, args[field]);
10671055
}
10681056
return this.send_command('hmset', tmp_args, callback);
10691057
}
1070-
return this.send_command('hmset', utils.to_array(arguments));
1058+
var len = arguments.length;
1059+
var tmp_args = new Array(len);
1060+
for (var i = 0; i < len; i += 1) {
1061+
tmp_args[i] = arguments[i];
1062+
}
1063+
return this.send_command('hmset', tmp_args);
10711064
};
10721065

10731066
Multi.prototype.hmset = Multi.prototype.HMSET = function (key, args, callback) {
@@ -1096,8 +1089,12 @@ Multi.prototype.hmset = Multi.prototype.HMSET = function (key, args, callback) {
10961089
tmp_args.push(callback);
10971090
}
10981091
} else {
1099-
tmp_args = utils.to_array(arguments);
1100-
tmp_args.unshift('hmset');
1092+
var len = arguments.length;
1093+
var tmp_args = new Array(len);
1094+
tmp_args[0] = 'hmset';
1095+
for (var i = 0; i < len; i += 1) {
1096+
tmp_args[i + 1] = arguments[i];
1097+
}
11011098
}
11021099
this.queue.push(tmp_args);
11031100
return this;
@@ -1231,7 +1228,7 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
12311228
var index = 0;
12321229
var args;
12331230
var args_len = 1;
1234-
var callbackWithoutCB = function (err, res) {
1231+
var callback_without_own_cb = function (err, res) {
12351232
if (err) {
12361233
self.results.push(err);
12371234
// Add the position to the error
@@ -1243,7 +1240,7 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
12431240
// Do not emit an error here. Otherwise each error would result in one emit.
12441241
// The errors will be returned in the result anyway
12451242
};
1246-
var lastCallback = function (cb) {
1243+
var last_callback = function (cb) {
12471244
return function (err, res) {
12481245
cb(err, res);
12491246
callback(null, self.results);
@@ -1263,19 +1260,17 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
12631260
while (args = this.queue.shift()) {
12641261
var command = args.shift();
12651262
var cb;
1266-
// Improve the callback function generation by using new Function
1267-
// check https://github.com/petkaantonov/bluebird/blob/master/src/promisify.js
12681263
args_len = args.length - 1;
12691264
if (typeof args[args_len] === 'function') {
12701265
cb = this.callback(args.pop(), index);
12711266
} else {
1272-
cb = callbackWithoutCB;
1267+
cb = callback_without_own_cb;
12731268
if (typeof args[args_len] === 'undefined') {
12741269
args.pop();
12751270
}
12761271
}
12771272
if (callback && index === len - 1) {
1278-
cb = lastCallback(cb);
1273+
cb = last_callback(cb);
12791274
}
12801275
this._client.send_command(command, args, cb);
12811276
index++;

0 commit comments

Comments
 (0)