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(serialize): faster serialization and less bundle size #126

Merged
merged 8 commits into from
Mar 1, 2025
Merged
Changes from 5 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
86 changes: 32 additions & 54 deletions src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,42 @@
return a - b;
}

// Uses fast path to compare primitive values (string, number, bigint, boolean, null, undefined)
// Only symbol, function and object values need to be full serialized
return String.prototype.localeCompare.call(
toComparableString(a) ?? this.serialize(a),
toComparableString(b) ?? this.serialize(b),
this.serialize(a, false),
this.serialize(b, false),
);
}

serialize(value: any): string {
const type = value === null ? "null" : typeof value;
// @ts-ignore
const handler = this["$" + type];
return handler.call(this, value);
serialize(value: any, addQuotes = true): string {
if (value === null) {
return "null";
}

switch (typeof value) {
case "string": {
return addQuotes ? `'${value}'` : value;
}
case "bigint": {
return `${value}n`;
}
case "object": {
return this.$object(value);
}
case "function": {
return this.$function(value);
}
}

return String(value);
}

serializeObject(object: any): string {
const objString = Object.prototype.toString.call(object);

let objType = "";
const objectLength = objString.length;

// '[object a]'.length === 10, the minimum
if (objectLength < 10) {
objType = "unknown:[" + objString + "]";
} else {
// '[object '.length === 8
objType = objString.slice(8, objectLength - 1);
}
const objLength = objString.length;
const objType =
objLength < 10 // '[object a]'.length === 10, the minimum
? `unknown:${objString}`

Check warning on line 63 in src/serialize.ts

View check run for this annotation

Codecov / codecov/patch

src/serialize.ts#L63

Added line #L63 was not covered by tests
: objString.slice(8, -1); // '[object '.length === 8

if (
objType !== "Object" &&
Expand All @@ -71,14 +79,14 @@
throw new Error(`Cannot serialize ${objType}`);
}

const constructor = object.constructor.name;
const objectName = constructor === "Object" ? "" : constructor;
const constructorName = object.constructor.name;
const objName = constructorName === "Object" ? "" : constructorName;

if (typeof object.toJSON === "function") {
return objectName + this.$object(object.toJSON());
return objName + this.$object(object.toJSON());
}

return this.serializeObjectEntries(objectName, Object.entries(object));
return this.serializeObjectEntries(objName, Object.entries(object));
}

serializeObjectEntries(type: string, entries: Iterable<[string, any]>) {
Expand All @@ -97,14 +105,6 @@
return content + "}";
}

$string(string: any) {
return `'${string}'`;
}

$bigint(bigint: bigint) {
return `${bigint}n`;
}

$object(object: any) {
let content = this.#context.get(object);

Expand Down Expand Up @@ -155,17 +155,6 @@
}
}

for (const type of [
"symbol",
"boolean",
"number",
"null",
"undefined",
] as const) {
// @ts-ignore
Serializer.prototype["$" + type] = String;
}

for (const type of ["Error", "RegExp", "URL"] as const) {
// @ts-ignore
Serializer.prototype["$" + type] = function (val: any) {
Expand Down Expand Up @@ -201,14 +190,3 @@
}
return Serializer;
})();

function toComparableString(val: unknown): string | undefined {
if (val === null) {
return "null";
}
const type = typeof val;
if (type === "function" || type === "object") {
return undefined;
}
return String(val);
}