obj size estimate (v6)

Revision 6 of this benchmark created on


Preparation HTML

<script>
const objectSize = (obj) => {
  return JSON.stringify(obj).length
}

const objectSizeEstimate = (obj) => {
  if (typeof obj === 'object' && obj !== null) {
    let sum = 0;
    for (const key in obj) {
      if (obj.hasOwnProperty(key)) {
        sum += key.length;
        sum += objectSizeEstimate(obj[key]);
      }
    }
    return sum
  }
  if (typeof obj === 'string') {
    return obj.length
  }
  return 4;
}

let obj = {
  "result": [
    {
      "message": "Hello, Nolan! Your order number is: #53",
      "phoneNumber": "(879) 768-4088 x338",
      "phoneVariation": "+90 318 130 10 27",
      "status": "active",
      "name": {
        "first": "Mekhi",
        "middle": "Skyler",
        "last": "Mante"
      },
      "username": "Mekhi-Mante",
      "password": "6HsHlcI1XSrQdK2",
      "emails": [
        "Nathan_Aufderhar@example.com",
        "Kianna.Stiedemann15@example.com"
      ],
      "location": {
        "street": "420 The Causeway",
        "city": "Gottliebstead",
        "state": "Delaware",
        "country": "Niue",
        "zip": "17035",
        "coordinates": {
          "latitude": "-1.5848",
          "longitude": "149.8544"
        }
      },
      "website": "https://jubilant-classroom.info",
      "domain": "honest-classmate.biz",
      "job": {
        "title": "National Branding Orchestrator",
        "descriptor": "Legacy",
        "area": "Paradigm",
        "type": "Coordinator",
        "company": "Senger and Sons"
      },
      "creditCard": {
        "number": "4635048783160",
        "cvv": "685",
        "issuer": "maestro"
      },
      "uuid": "08092f0c-9957-4dcf-92ef-d886d9137efe",
      "objectId": "66d18419c6758160f2ab9b0d"
    }
  ]
};

let largeObj = {
	arr: [
		{...obj},
		{...obj},
		{...obj},
		{...obj},
		{...obj},
		{...obj},
		{...obj},
		{...obj},
	],
}
</script>

Setup

obj.r = Math.random()
largeObj.r = Math.random()

Test runner

Ready to run.

Testing in
TestOps/sec
JSON.stringify
objectSize(obj)
ready
estimate
objectSizeEstimate(obj)
ready
JSON.stringify large
objectSize(largeObj)
ready
estimate large
objectSizeEstimate(largeObj)
ready

Revisions

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