Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: avoid lowercasing long strings #81

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,30 @@ export function destr<T = unknown>(value: any, options: Options = {}): T {
return _value.slice(1, -1) as T;
}

const _lval = _value.toLowerCase();
if (_lval === "true") {
return true as T;
}
if (_lval === "false") {
return false as T;
}
if (_lval === "undefined") {
return undefined as T;
}
if (_lval === "null") {
// eslint-disable-next-line unicorn/no-null
return null as T;
}
if (_lval === "nan") {
return Number.NaN as T;
}
if (_lval === "infinity") {
return Number.POSITIVE_INFINITY as T;
}
if (_lval === "-infinity") {
return Number.NEGATIVE_INFINITY as T;
if (_value.length <= 9) {
const _lval = _value.toLowerCase();
if (_lval === "true") {
return true as T;
}
if (_lval === "false") {
return false as T;
}
if (_lval === "undefined") {
return undefined as T;
}
if (_lval === "null") {
// eslint-disable-next-line unicorn/no-null
return null as T;
}
if (_lval === "nan") {
return Number.NaN as T;
}
if (_lval === "infinity") {
return Number.POSITIVE_INFINITY as T;
}
if (_lval === "-infinity") {
return Number.NEGATIVE_INFINITY as T;
}
}

if (!JsonSigRx.test(value)) {
Expand Down