Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve sort performance by 150,000x
- Loading branch information
1 parent
54f4701
commit 7dd90e0
Showing
1 changed file
with
9 additions
and
5 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,16 +1,20 @@ | ||
import type {ParsedNumber} from '../types'; | ||
import {RE_DATE} from './regex'; | ||
|
||
const parseDate = (value: string): ParsedNumber | undefined => { | ||
if (RE_DATE.test(value)) { | ||
const parseDate = (value: string): ParsedNumber | void => { | ||
try { | ||
const parsedDate = Date.parse(value); | ||
|
||
if (!Number.isNaN(parsedDate)) { | ||
return parsedDate; | ||
if (RE_DATE.test(value)) { | ||
return parsedDate; | ||
} | ||
} | ||
} | ||
|
||
return undefined; | ||
return undefined; | ||
} catch { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export default parseDate; |
7dd90e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the idea is to avoid an expensive regexp operation that was running for every single string to check if a string is a date.
Instead, we first try to parse the string into a date, and only if JS thinks it's a date, we check does it conforms to the regexp.