Soft equal

Benchmark created on


Setup

const filterObject = (object) => {
        return Object.keys(object || {}).reduce((filteredObject, key) => {
            const value = object[key];
            const isEmptyArray = Array.isArray(value) && !value.length;

            if (
                value !== undefined &&
                value !== null &&
                !isEmptyArray &&
                value !== ""
            ) {
                filteredObject[key] = value;
            }

            return filteredObject;
        }, {});
    }

const isSoftEqual = (obj1 = {}, obj2 = {}) => {
        obj1 = filterObject(obj1);
        obj2 = filterObject(obj2);

        if (Object.keys(obj1).length !== Object.keys(obj2).length)
            return false;

        for (const p in obj1) {
            if (p in obj1) {
                const obj1Value = obj1[p];
                const obj2Value = obj2[p];

                switch (typeof (obj1Value)) {
                    // Deep compare objects
                    case "object":
                        if (!isSoftEqual(obj1Value, obj2Value)) return false;
                        break;
                    // Compare functions
                    case "function":
                        if (typeof (obj2Value) === "undefined" || (p !== "compare" && String(obj1Value) !== String(obj2Value))) return false;
                        break;
                    case "boolean":
                        if (obj1Value !== obj2Value) return false;
                        break;
                    // Compare values
                    default:
                        if ((!!obj1Value || !!obj2Value || obj1Value === 0 || obj2Value === 0) && (obj1Value !== obj2Value)) return false;
                }
            }
        }
        return true;
    }
    
    const isJSONEquals = (obj1 = {}, obj2 = {}) => {
    	obj1 = filterObject(obj1);
        obj2 = filterObject(obj2);
        return JSON.stringify(obj1) === JSON.stringify(obj2)
    };   
    
    const testObj = {
    "id": "2d404f4b-4e4d-42b4-9ce5-b079604b98ed",
    "organizationId": "6051013a-e968-42dd-bd70-acb198429911",
    "number": "0000000008",
    "numberPrefix": "OTL",
    "checkin": "2024-07-08T14:00:00",
    "checkout": "2024-07-09T12:00:00",
    "checkinMsLikeUtc": 1720447200000,
    "checkoutMsLikeUtc": 1720526400000,
    "discount": {
        "roubles": 0,
        "copecks": 0
    },
    "discountType": 0,
    "finalCost": {
        "roubles": 1000,
        "copecks": 0
    },
    "created": "2024-07-08T12:47:30.4396394+00:00",
    "modifiedAt": "2024-07-08T12:48:03.5432427+00:00",
    "bookingFeatures": [],
    "timeBoxes": [
        {
            "dateTime": "2024-07-08T00:00:00",
            "timeBoxFeatures": []
        },
        {
            "dateTime": "2024-07-09T00:00:00",
            "timeBoxFeatures": []
        }
    ],
    "finalLivingPricesByDays": [
        {
            "roubles": 1000,
            "copecks": 0
        }
    ],
    "finalLivingCost": {
        "roubles": 1000,
        "copecks": 0
    },
    "finalEarlyCheckin": {
        "roubles": 0,
        "copecks": 0
    },
    "finalLateCheckout": {
        "roubles": 0,
        "copecks": 0
    },
    "livingCost": {
        "roubles": 1000,
        "copecks": 0
    },
    "baseLivingCost": {
        "roubles": 1000,
        "copecks": 0
    },
    "totalPromoCodeDiscountAmount": {
        "roubles": 0,
        "copecks": 0
    },
    "pricesByDays": [
        {
            "roubles": 1000,
            "copecks": 0
        }
    ],
    "livingPricesByDays": [
        {
            "roubles": 1000,
            "copecks": 0
        }
    ],
    "transactionGroupCollection": [],
    "cancellation": {
        "isCanceled": false,
        "reason": ""
    },
    "arePriceAffectedFieldsChanged": false,
    "areRateFeatureAffectedFieldsChanged": false,
    "roomCategoryId": "66e086e3-40b0-4165-8128-4f5f0342a577",
    "rateId": "752cfab7-54ba-42dd-b000-e2060b9e3fa9",
    "placementId": "13d9d6bd-a289-4160-a541-a4469003da48",
    "adultsCount": 1,
    "guestInfos": [
        {
            "id": "c87778ac-78b6-42b6-899c-8956fed01c70",
            "isSettled": false,
            "fio": "fgdhfghfgjh",
            "phone": ""
        }
    ],
    "havePaymentCardInfo": false,
    "confirmationInfo": {
        "confirmationStatus": 0
    },
    "customerType": 1,
    "customer": {},
    "payer": {
        "payerType": "bookingGuest"
    },
    "source": "reception",
    "bookingStatus": 1,
    "isRequestToBook": false,
    "earlyCheckinPrice": {
        "roubles": 0,
        "copecks": 0
    },
    "latelyCheckoutPrice": {
        "roubles": 0,
        "copecks": 0
    },
    "includeEarlyCheckin": false,
    "includeLatelyCheckout": false,
    "paymentsInfo": {
        "reservation": {
            "paid": {
                "roubles": 0,
                "copecks": 0
            },
            "prepaid": {
                "roubles": 0,
                "copecks": 0
            }
        },
        "features": [],
        "restOfDeposit": {
            "roubles": 0,
            "copecks": 0
        },
        "restaurantFeaturesSum": {
            "roubles": 0,
            "copecks": 0
        },
        "totalPaid": {
            "roubles": 0,
            "copecks": 0
        }
    },
    "touristTaxData": [
        {
            "guestId": "c87778ac-78b6-42b6-899c-8956fed01c70",
            "daysCount": 0,
            "defaultDaysCount": 0,
            "isChildTillCheckout": false,
            "paidSum": {
                "roubles": 0,
                "copecks": 0
            },
            "isExempted": false
        }
    ],
    "restaurantFeatureSets": []
};

Teardown


Test runner

Ready to run.

Testing in
TestOps/sec
Soft equal
const isEquals = isSoftEqual(testObj, testObj);
ready
JSON equal
const isEquals = isJSONEquals(testObj, testObj);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.