Skip to content

Commit d411359

Browse files
committed
perf: improve performance of .hasFlag()
1 parent ae56f81 commit d411359

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

index.js

Lines changed: 9 additions & 11 deletions
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 has the flag
1724
*
@@ -22,20 +29,11 @@ exports.list = Object.keys(commands);
2229
* @public
2330
*/
2431
exports.hasFlag = function (commandName, flag) {
25-
var command = commands[commandName];
26-
if (!command) {
32+
if (!flags[commandName]) {
2733
throw new Error('Unknown command ' + commandName);
2834
}
2935

30-
var flags = command.flags;
31-
32-
for (var i = 0; i < flags.length; i++) {
33-
if (flags[i] === flag) {
34-
return true;
35-
}
36-
}
37-
38-
return false;
36+
return Boolean(flags[commandName][flag]);
3937
};
4038

4139
/**

0 commit comments

Comments
 (0)