Skip to content

Commit f9a1dcd

Browse files
committed
Merge pull request #2 from NodeRedis/perf/has-flag
perf: improve performance of .hasFlag()
2 parents de30977 + d411359 commit f9a1dcd

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

index.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ var commands = require('./commands');
1212
*/
1313
exports.list = Object.keys(commands);
1414

15+
var flags = {};
16+
exports.list.forEach(function (commandName) {
17+
flags[commandName] = commands[commandName].flags.reduce(function (flags, flag) {
18+
flags[flag] = true;
19+
return flags;
20+
}, {});
21+
});
1522
/**
1623
* Check if the command exists
1724
*
@@ -33,20 +40,11 @@ exports.exists = function (commandName) {
3340
* @public
3441
*/
3542
exports.hasFlag = function (commandName, flag) {
36-
var command = commands[commandName];
37-
if (!command) {
43+
if (!flags[commandName]) {
3844
throw new Error('Unknown command ' + commandName);
3945
}
4046

41-
var flags = command.flags;
42-
43-
for (var i = 0; i < flags.length; i++) {
44-
if (flags[i] === flag) {
45-
return true;
46-
}
47-
}
48-
49-
return false;
47+
return Boolean(flags[commandName][flag]);
5048
};
5149

5250
/**

0 commit comments

Comments
 (0)