Last active
April 18, 2022 03:39
-
-
Save cferdinandi/d99723a26316aa710e404fa9600ccc0b to your computer and use it in GitHub Desktop.
Testing localStorage performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.