Skip to content

Commit d61fa06

Browse files
authored
perf(index): use Object.keys over Object.entries for opt parsing (#505)
1 parent 8472e0a commit d61fa06

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

src/index.js

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -69,43 +69,44 @@ function parseOptions(acceptedOptions, options, version) {
6969
const invalidArgs = [];
7070

7171
// Imperative loops are faster than functional loops, see https://romgrk.com/posts/optimizing-javascript
72-
const entries = Object.entries(options);
73-
const entriesLength = entries.length;
74-
for (let i = 0; i < entriesLength; i += 1) {
75-
// Destructuring adds overhead, so use index access
76-
const key = entries[i][0];
77-
if (Object.hasOwn(acceptedOptions, key)) {
78-
const option = entries[i][1];
79-
const acceptedOption = acceptedOptions[key];
80-
81-
if (acceptedOption.type === typeof option) {
82-
// Skip boolean options if false
83-
if (acceptedOption.type !== "boolean" || option) {
84-
args.push(acceptedOption.arg);
85-
}
86-
} else {
87-
invalidArgs.push(
88-
`Invalid value type provided for option '${key}', expected ${
89-
acceptedOption.type
90-
} but received ${typeof option}`
91-
);
92-
}
72+
const keys = Object.keys(options);
73+
const keysLength = keys.length;
74+
for (let i = 0; i < keysLength; i += 1) {
75+
const key = keys[i];
76+
if (!Object.hasOwn(acceptedOptions, key)) {
77+
invalidArgs.push(`Invalid option provided '${key}'`);
78+
continue;
79+
}
9380

94-
/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
95-
if (lt(version, acceptedOption.minVersion)) {
96-
invalidArgs.push(
97-
`Invalid option provided for the current version of the binary used. '${key}' was introduced in v${acceptedOption.minVersion}, but received v${version}`
98-
);
99-
}
81+
// @ts-ignore: keys are from options, TS cannot infer this
82+
const option = options[key];
83+
const acceptedOption = acceptedOptions[key];
10084

101-
/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
102-
if (gt(version, acceptedOption.maxVersion || version)) {
103-
invalidArgs.push(
104-
`Invalid option provided for the current version of the binary used. '${key}' is only present up to v${acceptedOption.maxVersion}, but received v${version}`
105-
);
85+
if (acceptedOption.type === typeof option) {
86+
// Skip boolean options if false
87+
if (acceptedOption.type !== "boolean" || option) {
88+
args.push(acceptedOption.arg);
10689
}
10790
} else {
108-
invalidArgs.push(`Invalid option provided '${key}'`);
91+
invalidArgs.push(
92+
`Invalid value type provided for option '${key}', expected ${
93+
acceptedOption.type
94+
} but received ${typeof option}`
95+
);
96+
}
97+
98+
/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
99+
if (lt(version, acceptedOption.minVersion)) {
100+
invalidArgs.push(
101+
`Invalid option provided for the current version of the binary used. '${key}' was introduced in v${acceptedOption.minVersion}, but received v${version}`
102+
);
103+
}
104+
105+
/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
106+
if (gt(version, acceptedOption.maxVersion || version)) {
107+
invalidArgs.push(
108+
`Invalid option provided for the current version of the binary used. '${key}' is only present up to v${acceptedOption.maxVersion}, but received v${version}`
109+
);
109110
}
110111
}
111112
if (invalidArgs.length === 0) {

0 commit comments

Comments
 (0)