Skip to content

Commit

Permalink
perf: improve sort performance by 150,000x
Browse files Browse the repository at this point in the history
  • Loading branch information
vladholubiev committed Jun 1, 2022
1 parent 54f4701 commit 7dd90e0
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/utils/parseDate.ts
@@ -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;

1 comment on commit 7dd90e0

@vladholubiev
Copy link
Member Author

@vladholubiev vladholubiev commented on 7dd90e0 Jun 1, 2022

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.

Please sign in to comment.