Stop Using Local Storage: 5 Reasons to Kick the Habit

Navod Dilshan
Nerd For Tech
Published in
5 min readApr 9, 2024

--

What is Local Storage?

Let’s start with the basics: local storage is a new feature of HTML5 that basically allows you (a web developer) to store any information you want in your user’s browser using JavaScript. Simple, right?

In practice, local storage is just one big old JavaScript object that you can attach data to (or remove data from). Here’s an example of some JavaScript code that stores some of my personal info in local storage, echoes it back to me, and then (optionally) removes it:

// Storing personal info
localStorage.setItem('name', 'John');
localStorage.setItem('age', '30');

// Retrieving and displaying stored info
const name = localStorage.getItem('name');
const age = localStorage.getItem('age');
alert(`${name} is ${age} years old.`);

// Optionally remove stored info
localStorage.removeItem('name');
localStorage.removeItem('age');

If you run this code in your browser on a test HTML page, you’ll see an alert saying “John is 30 years old.” Neat, huh? Also, if you peek into your browser’s developer tools, you’ll find both ‘name’ and ‘age’ stored in local storage.

But wait, what if you don’t want to manually clean up stored data? Enter sessionStorage! It’s just like local storage, but data is automatically wiped when the user closes their browser tab. Huge shoutout to the HTML5 working group for this gem!

What’s Cool About Local Storage?

Now that we’ve got a handle on what local storage is, let’s delve into what makes it cool! Even though the main aim here is to steer you away from using local storage for session data, it still boasts some intriguing features.

First off, it’s all about JavaScript! Unlike cookies (the primary alternative), which require a web server for creation, local storage operates purely with JavaScript. No need to fuss with web servers — local storage lets your web pages function independently. If you’re crafting a static site like a single-page app, local storage empowers your pages to store data in the browser without relying on backend languages or server logic. That’s a game-changer and a big reason why developers love it.

Another perk? Local storage isn’t as cramped as cookies. It offers a minimum of 5MB data storage across all major browsers, a significant bump from the paltry 4KB limit imposed on cookies. This expanded capacity makes local storage ideal for caching application data in the browser for future use. With cookies maxing out at 4KB, local storage emerges as one of the few viable alternatives for handling larger datasets.

What’s the Problem with Local Storage?

Alright, time to address the downsides. Brace yourself for the bad news about local storage.

  1. It’s Super Basic: Local storage is about as basic as it gets. It can only handle string data. Yup, you heard that right. So, anything even remotely complex? Forget about it. Serializing everything, including data types, just to fit it into local storage? Talk about an ugly workaround.
  2. Synchronous Operations: Local storage operates synchronously. That means each operation happens one at a time. For complex applications, this spells trouble, slowing down your app’s runtime significantly.
  3. No Love for Web Workers: Here’s a bummer — local storage can’t be accessed by web workers. So, if you’re eyeing background processing for performance boosts or fancy chrome extensions, tough luck. Local storage won’t play nice with those.
  4. Size Constraints: Sure, local storage gives you about 5MB of space across major browsers. But for data-intensive apps or those needing to function offline, that’s not much room to maneuver.
  5. Zero Data Protection: Buckle up for the security risk. Any JavaScript snippet on your page can tap into local storage without breaking a sweat. No data protection measures in sight. Security nightmare alert!

To sum it up, only go for local storage if you’re storing public, non-sensitive info, not aiming for high-performance apps, staying under the 5MB limit, and dealing solely with string data.

If your app doesn’t fit the bill? Save yourself the headache and look elsewhere. We’ll dive into alternatives later.

Why You Shouldn’t Trust Local Storage with Your Secrets

Local storage may seem convenient, but when it comes to security, it’s like leaving your valuables in a cardboard box on the sidewalk. Here’s why you should never use local storage for sensitive data.

  1. Inherent Insecurity: Local storage wasn’t built with security in mind. It’s essentially an open book for any JavaScript code running on your website.
  2. JavaScript, the Double-Edged Sword: JavaScript is incredibly powerful, but that also makes it dangerous. When you store sensitive info in local storage, you’re essentially trusting the most potent tool on the web to safeguard your secrets.
  3. Cross-Site Scripting (XSS) Nightmare: XSS attacks are the stuff of web developers’ nightmares. If an attacker can execute JavaScript on your site (and trust me, they’ll find a way), they can easily access and steal all your local storage data, including sensitive stuff like user sessions or payment details.
  4. Third-Party Risks: Even if your site itself is a fortress, third-party scripts are the chink in your armor. If any script from external sources (think ad networks, analytics tools, or even popular libraries like jQuery) gets compromised, your local storage becomes an open buffet for attackers.
  5. Real-World Vulnerabilities: Let’s face it — in the real world, avoiding third-party scripts is easier said than done. Marketing teams often handle website content using various tools, opening the door to sneaky third-party JavaScript injections without your knowledge.

To keep your users’ data safe and your sanity intact, steer clear of local storage for anything sensitive. It’s just not worth the risk.

What is the Alternative?

Given the vulnerabilities of local storage, it’s crucial to explore better alternatives. Let’s dive into what you should use instead.

Sensitive Data

For anything sensitive — like user IDs, session IDs, personal info, credit card details, API keys, or anything you wouldn’t want plastered on social media — always opt for a server-side session.

Here’s how:

  • Upon user login, generate a session identifier and store it in a cryptographically signed cookie.
  • Ensure the cookie library sets the httpOnly flag (for secure server-side sessions), SameSite=strict flag (to thwart CSRF attacks), and secure=true flag (for encrypted connections).
  • Retrieve user account details using the session ID for every request, fetching data from a database or cache.
  • Keep it simple, secure, and scalable. Server-side sessions are the way to go, even for large websites.

Non-String Data

If you need to store complex data in the browser — like integers, floats, etc. — look no further than IndexedDB. It provides a database-like object store, allowing you to define primary keys, handle indexing, and ensure data integrity.

Offline Data

When offline functionality is a must, combine IndexedDB with the Cache API, a part of Service Workers.

Here’s the breakdown:

  • Use IndexedDB to store structured data.
  • Employ the Cache API to cache network resources your app needs to function offline.
  • Dive into Google’s tutorial on the Cache API for in-depth insights.

By leveraging these alternatives, you can sidestep the pitfalls of local storage and ensure your data remains secure, structured, and accessible — without compromising on performance or user experience.

Remember, the security and performance of your application depend on making informed choices about data storage. So, steer clear of local storage when it’s not suitable, and opt for more secure and appropriate alternatives instead.

Happy coding :)

--

--