Skip to content

Commit

Permalink
perf(serialize): faster Date serialization (#130)
Browse files Browse the repository at this point in the history
* perf(serialize): use toISOString for faster Date serialization

* fix(serialize): handle invalid `Date`
  • Loading branch information
zsilbi authored Mar 2, 2025
1 parent b8a0cca commit baea38d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ const Serializer = /*@__PURE__*/ (function () {
}

$Date(date: any) {
return `Date(${date.toJSON()})`;
try {
return `Date(${date.toISOString()})`;
} catch {
return `Date(null)`;
}
}

$ArrayBuffer(arr: ArrayBuffer) {
Expand Down
3 changes: 3 additions & 0 deletions test/serialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ describe("serialize", () => {
expect(serialize(new Date(0))).toMatchInlineSnapshot(
`"Date(1970-01-01T00:00:00.000Z)"`,
);
expect(serialize(new Date(Number.NaN))).toMatchInlineSnapshot(
`"Date(null)"`,
);
});

it("boolean", () => {
Expand Down

0 comments on commit baea38d

Please sign in to comment.