Monthly Archives: October 2013

Wishing for Memory Functions in JavaScript

Share this:  

At work I am working on a web app that is very data intensive. A whole lot is done on the client side in JavaScript. There are certain instances in tests I have done where I have run out of memory.

I can adjust parameters down until the errors go away but:

  • Am I getting the absolute best usage of my memory or am I “leaving my change on the table?”
  • What if a user’s PC has less memory than me, are they going to get out of memory errors?
  • What if a user’s PC has a lot more memory than I do… then they really will not be able to leverage that extra memory to their advantage.

I wish there were two JavaScript functions available for programmers to use:

  • totalMemory()
  • availableMemory()

The functions could be called something else, that’s fine. But if there were these functions and if they returned the number of bytes relative to what was accessible to that current instance of the web browser, that would be so useful! As far as I know, JavaScript does not have any functions like this. I’ve looked 🙂

Am I missing something? Are there functions or object properties that I’ve missed? If you are a web developer working on some data intensive stuff on the client side don’t you wish you had these functions available?

One More Memory Related Thing
One more thing regarding JavaScript memory usage… There is a piece of info that I do know is out there somewhere… but I have not found yet… How much memory do JavaScript variables use?

I assume strings  may use 1 byte per character (or do they use 2 bytes to support unicode). I assume booleans take the least amount of memory (possibly 1 bit)? There are integers and floating point values… how many bytes do they use each? What is the overhead number of bytes for each variable and for each element in an array? Knowing this information could be helpful in deciding how to store information in memory the most efficient way.

UPDATE:
Microsoft says that their floating point variables in JavaScript use 8 bytes (search for the word “byte” on the page).
see: http://msdn.microsoft.com/en-us/library/ie/7wkd9z69(v=vs.94).aspx

UPDATE  2:
This looks like an interesting JS library: something where you pass in a variable or JSON string and it returns how many bytes it takes up.  I have to investigate this further when I have some time!
http://code.stephenmorley.org/javascript/finding-the-memory-usage-of-objects/