Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Last active April 18, 2022 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/d99723a26316aa710e404fa9600ccc0b to your computer and use it in GitHub Desktop.
Save cferdinandi/d99723a26316aa710e404fa9600ccc0b to your computer and use it in GitHub Desktop.
Testing localStorage performance
<!DOCTYPE html>
<html>
<head>
<title>localStorage Performance Test</title>
</head>
<body>
<script>
// Number of items to store
var count = 10000;
// Setup data
var data = {
items: []
};
// Create data array
for (var i = 0; i < count; i++) {
data.items.push(i);
}
console.log('data: ', data);
/**
* Start Performance Test
*/
var start = performance.now();
// Set/get data to localStorage
localStorage.setItem('perfTest', JSON.stringify(data));
var cache = localStorage.getItem('perfTest');
cache = cache ? JSON.parse(cache) : {};
var end = performance.now();
console.log('It took ' + (end - start) + 'ms.');
</script>
</body>
</html>
@benwills
Copy link

Just a heads up that this is also adding the cost of JSON.stringify(). Most of the time, you will only be getting data from localStoage and parsing it.

Removing JSON.stringify(), for me, took the performance from 11ms to 1.5ms. That's pretty significant in terms of what's being tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment