-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: freeze parse options to avoid object allocation
- Loading branch information
Showing
1 changed file
with
33 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,41 @@ | ||
const var1 = Object.freeze({ includePrerelease: true, loose: true, rtl: true }); | ||
const var2 = Object.freeze({ includePrerelease: true, loose: true }); | ||
const var3 = Object.freeze({ includePrerelease: true, rtl: true }); | ||
const var4 = Object.freeze({ includePrerelease: true }); | ||
const var5 = Object.freeze({ loose: true, rtl: true }); | ||
const var6 = Object.freeze({ loose: true }); | ||
const var7 = Object.freeze({ rtl: true }); | ||
const emptyOpts = Object.freeze({}); | ||
|
||
const parseOptions = options => { | ||
if (!options) return {}; | ||
if (!options) return emptyOpts; | ||
|
||
if (typeof options !== 'object') return var6; | ||
|
||
if (typeof options !== 'object') return { loose: true }; | ||
if (options.includePrerelease) { | ||
if (options.loose && options.rtl) { | ||
return var1; | ||
} | ||
|
||
const parsedOptions = {}; | ||
if (options.loose) { | ||
return var2; | ||
} | ||
|
||
// parse out just the options we care about so we always get a consistent | ||
// obj with keys in a consistent order. | ||
if (options.rtl) { | ||
return var3; | ||
} | ||
|
||
if (options.includePrerelease) parsedOptions.includePrerelease = true; | ||
if (options.loose) parsedOptions.loose = true; | ||
if (options.rtl) parsedOptions.rtl = true; | ||
return var4; | ||
} else if (options.loose) { | ||
if (options.rtl) { | ||
return var5; | ||
} | ||
|
||
return parsedOptions; | ||
return var6; | ||
} else if (options.rtl) { | ||
return var7; | ||
} else { | ||
return emptyOpts; | ||
} | ||
}; | ||
module.exports = parseOptions; |