I'm creating a single player Web rpg that will store a decent amount of information about the player, items, map, npcs, etc.

I have been developing html5, javascript, php, and the mysql database approach and saving/retrieving with json.

Someone mentioned for my purposes, using 'localStorage' persistent user storage would remove much complexity and help with performance.

Is localStorage the better way to go here? If I wanted to find out information about my players, or ensure that esch player profile is saved... can I rely on localStorage?

What are the pros and cons of each?

  • \$\begingroup\$ What are you doing with the PHP? If you use local storage I think you'd be able to do the entire game offline and client side \$\endgroup\$
    – ssb
    Commented May 15, 2014 at 6:39
  • \$\begingroup\$ How can localStorage and PHP + MySQL even compete? The first represents storage on the client side and the second represents storage on your server. Two very different things, so you'll have to decide where data has to be stored and why first. \$\endgroup\$
    – user15805
    Commented May 15, 2014 at 6:41

1 Answer 1


window.localStroage is a more modern alternative to cookies. It allows you to store (semi-)persistent data in the users web browser which will survive a browser restart. The client-sided javascript can access it without having to consult a server, which makes it quite fast to access from the client. But contrary to cookies, localstorage is not directly accessible for the server. When you need data from localstorage on the server, you need to send it to the server via AJAX or websockets after the website has loaded.

localStorage has a much higher capacity than cookies, but not unlimited. The standard suggests 5MB, but that's just a suggestion. Most browsers have a higher limit and react differently when it is exceeded. Some ask the user for permission to store more, some just drop any excess data. But you can not be sure how the browser reacts, so you should try to stay below the 5MB limit.

Keep in mind that localStorage doesn't guarantee persistence. The user can accidentally or intentionally delete their localstorage, just like they can delete cookies. Also, the web browser might have a combined hard-drive quota for localstorage for all websites and delete arbitrary data when running out.

The localstorage also doesn't guarantee integrity. The user can manipulate their localstorage. That means when you store any game state in it, you have to assume that the user will be able to edit it in any way they want, allowing them to cheat. When your game is single-player, that might not be that much of an issue. But when your game has any interaction between players (and when it's just a public scoreboard), cheaters might be quite an annoyance for the more honest players.

However, localstorage has the advantage over server-sided persistence that you do not need personalized user accounts. The data is bound to the web browser installation, so the user can save their game without having to register and log into an account. You also don't need a server-sided database which greatly simplifies your server infrastructure.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .