3

I know I can get the size of a JSON object in bytes by using JSON.parse(data).length.
//UTF-8 etc can be ignored for now but parsing takes time for huge data which I don't want.

Is there any way to get its size in MB without transforming it to a string?

Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
Arvind
  • 1,207
  • 6
  • 27
  • 55
  • Possible duplicate of [get size of json object](https://stackoverflow.com/questions/6756104/get-size-of-json-object) – Arber Sylejmani Jan 23 '19 at 14:18
  • What do you mean with "size", what do you want to know? number of elements of the object? – ccordon Jan 23 '19 at 14:23
  • 1
    No this is not duplicate, I want to know how much memory is required to hold this object in MB – Arvind Jan 23 '19 at 14:36
  • 1
    Possible duplicate of [How to know size of response in ajax request](https://stackoverflow.com/questions/30640418/how-to-know-size-of-response-in-ajax-request) – Craicerjack Jan 23 '19 at 14:46
  • If you need the object size just for your own knowledge and not using it in the code, then try looking in the [Chrome Heap Profiler](https://developer.chrome.com/devtools/docs/heap-profiling?hl=sv). – Jimenemex Jan 23 '19 at 15:14
  • I need it in runtime to decide the next course of action – Arvind Jan 24 '19 at 06:11
  • 2
    @Arvind If my answer was useful to you, you can accept it or qualify it so other users with a similar problem can find a solution faster. – ccordon Jan 24 '19 at 22:30

3 Answers3

1

There is no native way to calculate the size of an object in Javascript but there is a node module that gives you the size of an object in bytes. object-sizeof

This would be an example of what you need:  

var sizeof = require('object-sizeof');

// 2B per character, 6 chars total => 12B

console.log(`${sizeof({abc: 'def'})/1024} MB`);
ccordon
  • 1,072
  • 12
  • 22
1

We have next options:

  • Recurring calculation like object-sizeof library
  • object to string/buffer:
    1. JSON.stringify({a:1}).length
    2. v8.serialize({a:1}).length
Yaroslav Gaponov
  • 1,997
  • 13
  • 12
1

For security reasons, Javascript is not allowed to access or mutate information about the device, therefore, determining exactly how many bytes an object occupies should be impossible.

With that being said, the following Javascript command DOES exist (within chrome only):

window.performance.memory

This returns an object with the amount of bytes the window can use at maximum, the amount of bytes used including free space, and the amount of bytes actually used. You could, theoretically, use that to determine the amount of bytes used before an object was created and after, and calculate the difference. The memory-stats project for instance utilizes that command.

However, the values in this object never change except if chrome was launched with the "--enable-precise-memory-info" flag. You therefore cannot use this command in a (production) application (the MDN docs indicate the same). You can only approach the amount of memory an object occupies by counting all the strings and numbers and multiplying that by how much bytes a string usually occupies (which is what the object-sizeof library does).

If you are just interested in the size of the object and do not wish to use that information in a production app, you can simply do so by making a timeline recording in the Chrome Devtools.

Robb216
  • 554
  • 4
  • 10