Can localStorage slow down my site when used frequently?

I am developing an HTML5 game and I need to know if updating localStorage properties can often slow down the page.

I actually save my heroโ€™s position in the four localStorage properties (two for the actual position and two for the previous position for use in the collision detection system) and updating it every 1 second , but I want to update it at 60 frames per second to save each hero movement.

Using localStorage at this frequency can lead to performance problems?

+7
javascript html5 local-storage canvas
source share
2 answers

Local storage stores data on the user's hard drive. It takes a little longer to read and write to the hard drive than in RAM.

The conclusion from this is that you can optimize your performance by reading local storage at startup and only writing it when the user logs out.

Now, whether this optimization will significantly affect your project, you have to figure it out, and as R3tep said, http://jsperf.com/ is a good solution.

My advice, however, is simply to go with optimization anyway, simply because it is less โ€œsatisfying,โ€ I think the program would run slower than it could for no good reason.

+3
source share

Save your data in the {} object and save it in locatlStorage , then use the I / O inactive or when the user leaves ( 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; 

PS Check storage in var with storage. Storage synchronization is more expensive.

+1
source share

All Articles