Determining HTML5 Database Memory Usage

I am adding sqlite support for my Google Chrome extension for storing historical data.

When creating a database, you need to set the maximum size (I used 5 MB, as suggested in many examples)

I would like to know how much memory I use (for example, after adding 1000 entries) in order to have an idea of ​​reaching the 5 MB limit and act accordingly.

The Chrome console does not show such numbers. Thanks.

+2
source share
2 answers

You can calculate these numbers if you want. Basically, the default limit for localStorage and webStorage is 5 MB, where the name and values ​​are stored as UTF16, so it really is half what is 2.5 MB in terms of stored characters. In webStorage, you can increase this by adding "unlimited_storage" to the manifest.

The same applies to WebStorage, but you need to go through all the tables and find out how many characters in each row.

In localStorage, you can verify this by running the population script:

var row = 0; localStorage.clear(); var populator = function () { localStorage[row] = ''; var x = ''; for (var i = 0; i < (1024 * 100); i++) { x += 'A'; } localStorage[row] = x; row++; console.log('Populating row: ' + row); populator(); } populator(); 

The above should crash on line 25 if there is not enough space, which is about 2.5 MB. You can invert and count the number of characters in a string and determine how much space you have.

Another way to do this is to always add a “payload” and check for an exception, if it exists, if so, then you know that you are out of space.

 try { localStorage['foo'] = 'SOME_DATA'; } catch (e) { console.log('LIMIT REACHED! Do something else'); } 

Internet Explorer did something called "remaining space", but it doesn’t work in Chrome / Safari: http://msdn.microsoft.com/en-us/library/cc197016(v=VS.85).aspx

+2
source

I would like to add a sentence.

If this is a Chrome extension, why not use a Web SQL store or indexed database?

http://html5doctor.com/introducing-web-sql-databases/

http://hacks.mozilla.org/2010/06/comparing-indexeddb-and-webdatabase/

Source: http://caniuse.com/

0
source

Source: https://habr.com/ru/post/1415323/


All Articles