Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to save to the browser localstorage 25 times per second?

I think the title is pretty much descriptive. I'm working on a idle game project using Ember.js and for now I'm saving to the localstorage 25 times per second instead of saving every 10 or 20 seconds. It's a really small amount of data and I'm not experiencing any performance issue (at least in my machine), but I'd like to know if this is bad and if so, how bad it is. Thanks :)

like image 829
Gustavo Siqueira Avatar asked Mar 02 '14 19:03

Gustavo Siqueira


1 Answers

There are two things :

1 ) Each browser supports different intervals of getting or setting localstorage values

If you performs too many times within a period the browser may hang and become unresponsive!

For best performance you should save to a local variable then periodically save it to localstorage.

Here are the list of browser's and their time taken to first read or write:

  • On Chrome, the first read takes ~1ms and subsequent reads take 0ms.
  • On Firefox, the first read takes ~0.5ms and subsequent reads take ~0.1ms.
  • On Internet Explorer 9, all reads take 0ms. I’m not sure how Internet Explorer is loading this data, but it doesn't appear to be the same as with other browsers.

  • On Opera, first read takes ~1ms and subsequent reads take 0ms.

  • For Safari on iOS 6, the very first read takes up to 24ms and subsequent reads take 0ms.

2 ) It also depends on the size of values that you are getting/setting.

Remember :

-localstorage has a limited capacity per domain and browser.

- And avoid to use localstorage while your page loads.

As my opinion : Give maximum time to localstorage if you don't want to crssh your game while user's are playing.

And calculate that maximum amount of data is used while user play the game and set time to write localstorage.

like image 189
ashbuilds Avatar answered Sep 19 '22 21:09

ashbuilds