Memory-Optimized Javascript Objects

Viran Malaka
4 min readDec 2, 2020

Once I was thinking about an in-memory database on nodejs, I just wanted to know what is the optimal way to keep our necessary data objects in the memory. When I was googling this I found a cool npm package called 'object-sizeof'. This is way more than a package that you can learn a lot more weird things about javascript. Without further talks, let's dig into awsome javascript world!

Environment setup.

I have created a simple ReactJS application as a playground for this article. Please feel free to fork this and try this out by your own. https://stackblitz.com/edit/object-sizeof-test?file=src/App.js

Fundamental memory allocation on javascript objects.

There are mainly 3 types of javascript variables. They are ‘Strings’, ‘Numbers’, ‘Object’. What…!!! is that it?. No Functions, Symbols and Regex are also data types in javascript, but I’m not going to talk about them because they are not used normally for storing data.

Example 01

As we discussed in the previous section, the numbers take 8 Bytes in memory. That means,

const a = 1;
const b = 999999999999999999999999999999999999999999999999;

both these variables have the same space on memory. Interesting right?

What to learn: Storing small number instead of larger numbers won't make your data store lesser in size.

See there no different in size regarding the value of the number

A practical use-case for what we have learned: Imagin you want to save some timestamps on an object and you are thinking that to reduce the amount of memory taken by that object, save relative timestamps instead of absolute timestamps by reducing a constant amount. But keep in mind, that will not reduce the sized of the object.

Example 02

When it comes to string values the length of the values must be considered. Check the following example.

See the memory differences between those two objects. The value of the second object has only one character more than the first one.

What to learn: The length of the string can make a considerable difference for the memory allocation.

Practical example: Assume we want to keep the country of a user as a string. Instead of saving them as ‘Sri Lanka’ or ‘Italy’ keep them as a shorter form like ‘LKR’ and ‘ITL’

Example 03

We know the keys of an object are strings. So maintaining smaller length for the keys may reduce the sized of the object.

Having a meaningful key may make your life easier as a programmer, but when you are really caring about memory, keep this in mind.

Example 04

This is the weirdest example I have encountered. Go through the following example first.

Holy cow! Were you able to get the point? Yes, keeping a single length string is the most memory optimal way rather than keeping a boolean value. Refer: https://dev.to/shevchenkonik/memory-size-of-javascript-boolean-3mlj

Example 05

Make your date more flatten, It will reduce the extra key overhead. Yes, I agree, this option will not save you more memory than keeping your data more readable. Totally up to you to take the decision.

I gathered this information and examples from the google search I have done. Please correct me if I was wrong at some point, and add any other weird cases you have encountered. Really appreciate your feedback. Thanks.

--

--