Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can localStorage slow down my website when used frequently?

I'm developing a HTML5 game and I need to know if updating localStorage properties frequently can slow down the page.

I'm actually storing my hero's position in four localStorage properties (two for the actual position and two for the past position to use in a collision detection system) and updating it every 1 second interval, but I want to update it at 60fps to save every hero movement.

Using localStorage in that frequency can result in performance issues?

like image 262
Tiago Marinho Avatar asked May 15 '14 11:05

Tiago Marinho


People also ask

Does localStorage affect performance?

Local storage stores the data on your user's hard drive. It takes a bit longer to read and write to the hard drive than it does to RAM. The conclusion to take away from this is that you could optimize your performance by reading from local storage on start up and only write to it when the user logs out.

Why we should not use localStorage?

If an attacker can run JavaScript on your website, they can retrieve all the data you've stored in local storage and send it off to their own domain. This means anything sensitive you've got in local storage (like a user's session data) can be compromised.

How much local storage can a website use?

It is limited to about 5MB and can contain only strings. Because it is tab specific, it is not accessible from web workers or service workers. LocalStorage should be avoided because it is synchronous and will block the main thread. It is limited to about 5MB and can contain only strings.

Is it good practice to use localStorage?

In basic terms, local storage enables developers to store and retrieve data in the browser. It is critical to understand, though, that using localStorage as a database for your project is not a good practice, since data will be lost when the user clears the cache, among other things.


2 Answers

Save your data to object {} and save it to locatlStorage then use I/O don't active or when user going away (onunload event):

var DATA = {},
    syncTimer;

function syncFunction () {
    localStorage.set('myData',JSON.stringify(DATA));
}


function someHandler() {
    // some handler that change your DATA
    // which can be called many times per second

    //if calling many times, data will not sync
    if (syncTimer) {
        clearTimeout(syncTimer);
    }

    //change data
    DATA.somefield = 'some data';

    //set timer if data not changed - save it
    syncTimer = setTimeout(syncFunction, 100)
}

window.onunload = syncFunction;

P.S. Test saving to var with saving to storage. Storage sync is more expensive.

like image 84
Pinal Avatar answered Oct 27 '22 00:10

Pinal


Local storage stores the data on your user's hard drive. It takes a bit longer to read and write to the hard drive than it does to RAM.

The conclusion to take away from this is that you could optimize your performance by reading from local storage on start up and only write to it when the user logs out.

Now, whether or not that optimization will significantly affect your project is something you'll have to figure out, and, as R3tep said, http://jsperf.com/ is a good solution.

My advice, though, is to just go with the optimization anyway, just because it's less "satisfying", I guess, to have a program run more slowly than it could for no good reason.

like image 43
Meredith Avatar answered Oct 26 '22 22:10

Meredith