2 ms·
Linked snipped will overflow localstorage in ~year of daily use and might take hundreds of miliseconds to parse multi megabyte json on every page load. I do thi
by rasz 5y ago
Linked snipped will overflow localstorage in ~year of daily use and might take hundreds of miliseconds to parse multi megabyte json on every page load. I do this in my version to prune:
function checkquota(){
// localStorage limit is ~5MB, should probably do something about exceeding quota ;)
// lets trigger around 3MB
// deleting half of the oldest empty(seen, never interacted with) entries first
// popping a warning when we finally reach over 2MB non empty
//
// total entries: localStorage.length
// storage bytes left: 1024 * 1024 * 5 - escape(encodeURIComponent(JSON.stringify(localStorage))).length
// non empty entries: Object.entries(localStorage).filter(value => value[1] != "1")
// non empty entries size: escape(encodeURIComponent(JSON.stringify(Object.entries(localStorage).filter(value => value[1] != "1")))).length
// empty entries sorted, oldest first Object.entries(localStorage).filter(value => value[1] == "1").sort(function (a, b) {return parseInt(a[0]) - parseInt(b[0]);});
//size check is expensive, can be as long as 500ms for full localstorage, limit size checks to ~once per 24h
if (GM_getValue("lastquotacheck") > Date.now()) return;
GM_setValue("lastquotacheck", Date.now()+86400000);
let size = encodeURIComponent(JSON.stringify(localStorage)).length;
console.log("Current size: "+size+" bytes");
if (size > 1024 * 1024 * 3)
{
alert("Current hackahackernews consumed localStorage size: "+size+" bytes, cleaning older entries to make more room.");
let sortedStorage = Object.entries(localStorage).filter(value => value[1] == "1").sort(function (a, b) {return parseInt(a[0]) - parseInt(b[0]);});
let cutout = parseInt(sortedStorage.length/2);
for (let [index, value] of sortedStorage.entries())
{
if (index < cutout)
{
localStorage.removeItem(value[0]);
}
}
size = encodeURIComponent(JSON.stringify(localStorage)).length;
console.log("Size after culling empty entries: "+size+" bytes");
let nonempty = Object.entries(localStorage).filter(value => value[1] != "1");
if (encodeURIComponent(JSON.stringify(nonempty)).length > 1024 * 1024 * 2)
{
console.log("Non empty entries size is over 2MB! currently at: "+encodeURIComponent(JSON.stringify(nonempty)).length+" bytes.");
if (confirm("Saved visited links are consuming "+encodeURIComponent(JSON.stringify(nonempty)).length+" bytes, delete half of the oldest entries?"))
{
let sortedNonempty = nonempty.sort(function (a, b) {return parseInt(a[0]) - parseInt(b[0]);});
let cutout = parseInt(sortedNonempty.length/2);
for (let [index, value] of sortedNonempty.entries())
{
if (index < cutout)
{
localStorage.removeItem(value[0]);
}
}
}
}
size = encodeURIComponent(JSON.stringify(localStorage)).length;
console.log("Final size after culling: "+size+" bytes");
}
}