Files
Tom Moor 4e807ca6fc perf: Copy only the patched path when recreating transforms (#14250)
* perf: Copy only the patched path when recreating transforms

recreateTransform deep copied the whole document for each patch
operation, and diffArrays compared elements with a generic isEqual. Copy
only the containers on the operation path and compare with a JSON-only
equality check. Revision changesets are about 6x faster.

* Use fast-deep-equal in place of a custom JSON equality check

* Restore isJSONEqual, fast-deep-equal throws on null-prototype attrs
2026-10-22 20:33:22 -04:00

40 lines
1.5 KiB
TypeScript

import { isJSONEqual } from "./util";
describe("isJSONEqual", () => {
it("should compare primitives", () => {
expect(isJSONEqual("a", "a")).toBe(true);
expect(isJSONEqual(1, 1)).toBe(true);
expect(isJSONEqual(Number.NaN, Number.NaN)).toBe(true);
expect(isJSONEqual(null, null)).toBe(true);
expect(isJSONEqual("1", 1)).toBe(false);
expect(isJSONEqual(null, undefined)).toBe(false);
expect(isJSONEqual(null, {})).toBe(false);
});
it("should compare arrays", () => {
expect(isJSONEqual([1, [2, 3]], [1, [2, 3]])).toBe(true);
expect(isJSONEqual([1, 2], [2, 1])).toBe(false);
expect(isJSONEqual([1], [1, 2])).toBe(false);
expect(isJSONEqual([], {})).toBe(false);
expect(isJSONEqual({}, [])).toBe(false);
});
it("should compare objects regardless of key order", () => {
expect(isJSONEqual({ a: 1, b: { c: [1] } }, { b: { c: [1] }, a: 1 })).toBe(
true
);
expect(isJSONEqual({ a: 1 }, { a: 2 })).toBe(false);
expect(isJSONEqual({ a: 1 }, { a: 1, b: 2 })).toBe(false);
expect(isJSONEqual({ a: undefined }, { b: undefined })).toBe(false);
});
it("should compare objects with a null prototype", () => {
const attrs = (level: number) =>
Object.assign(Object.create(null) as Record<string, unknown>, { level });
expect(isJSONEqual(attrs(1), attrs(1))).toBe(true);
expect(isJSONEqual(attrs(1), { level: 1 })).toBe(true);
expect(isJSONEqual(attrs(1), attrs(2))).toBe(false);
});
});