Skip to content

Instantly share code, notes, and snippets.

@martynchamberlin
Last active September 12, 2016 20:39
Show Gist options
  • Save martynchamberlin/d55d0f721cc2dd3f6b4b007368c24da4 to your computer and use it in GitHub Desktop.
Save martynchamberlin/d55d0f721cc2dd3f6b4b007368c24da4 to your computer and use it in GitHub Desktop.
Benchmarking localStorage in small big versus big small
<script>
// Store and retrieve 1000 100-byte items. Then store and retrieve 10 10000-byte item
let test = (numBlocks, byteSze) => {
let bytes = 'a';
for (let i = 0; i < byteSze; i++) {
bytes += 'a';
}
for (let i = 0; i < numBlocks; i++) {
localStorage.setItem(`test${i}`, bytes);
}
for (let i = 0; i < numBlocks; i++) {
localStorage.getItem(`test${i}`);
}
}
test(1000, 100);
localStorage.clear();
// Run this once, record the data, then comment the stuff above, uncomment the stuff below, and run again
// test(10, 10000);
// localStorage.clear();
</script>
@martynchamberlin
Copy link
Author

martynchamberlin commented Sep 12, 2016

Turns out that doing a lot of retrievals of small localStorage items is less efficient than doing fewer retrievals of large localStorage items. The takeaway is that if you have the choice of storing 10 items in localStorage separately versus in a conglomerate object, the latter is a better idea from a performance standpoint.

Here are my results on Chrome 52 on a late 2015" MBP with 16GB RAM and 256GB SSD running El Capitan.

test-1
The results of test(1000, 100)

test-2
The results of test(10, 10000)

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