I'm writing a web server as a test project in node.js

Whats really bothering me so far is the lack of control or even awareness of memory usage. Naturally I want to cache some stuff in RAM for faster operations but how much are my objects using? There seems to be no reliable way to tell.

Best I can think up is estimating worst case scenarios, each reference 8B, each character in a string 2B but what is the base cost of an array or an object? Could be nothing or could be something depending on how they are implemented, I've got no idea.

I tried measuring the sizes of things by creating 10M of them and measuring the difference before and after with forcing GC and process.memoryUsage().heapUsed but got some really odd results, Numbers would be 10B, each character in a string 5B, an empty array 42B, and empty object 66B, I doubt its correct but its the only result I have.

At this point I'm thinking that creating a huge blob and manually converting js@v8 native types into bytes and storing them there is the only way to actually have a clue, sounds pretty lame.

Is there a practical way to actually utilize the physical memory my server near its limit without the fear of running out?

  • 2
    I'm not sure node.js is really the right environment if you want that level of control. That's more the domain of C/C++ where you know exactly how every data structure is organized and stored in memory. Somewhat by design, data types are abstract in node.js as is garbage collection. In fact, arrays in V8 can be stored multiple ways depending upon how they are structured to optimize for speed or compactness based on how it's being used. Likewise objects have various fast access optimizations that may or may not be present depending upon circumstances.
    – jfriend00
    Commented Feb 15, 2020 at 0:29
  • 1
    You can't even really know when a bunch of objects you declared, used and then no longer have references to have been fully garbage collected. Another thought is that you may consider out of process storage mechanisms such as a database (that does its own caching and manages its own memory usage for you) or if you don't need persistence, you can use a memory store like redis that manages things for you.
    – jfriend00
    Commented Feb 15, 2020 at 0:30

1 Answer 1


You may find this tool useful: sizeof.js.

Also, this answer, and some of its comments seem to be the perfect solution to your problem.

  • The SizeOf link is now broken
    – Peter M
    Commented Mar 29, 2021 at 14:02
  • @PeterM -- Sorry. Page is in Wayback machine, but alas, the code is not. Internet rot strikes again! Please upvote my answer and the maintenence of it! We work hard for those scraps of points we actually get! Thank you! Commented Mar 30, 2021 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.