1 function env(strategy) { 2 const _strategy = window[strategy]; 3 function getStr(size) { 4 return Array(size + 1).join("1"); 5 } 6 7 function test(pieces, size) { 8 let count = 0, 9 piece = pieces.shift(), 10 trySize = size; 11 if (pieces.length) { 12 try { 13 while (true) { 14 trySize = size + (count + 1) * piece; 15 _strategy.setItem(`${strategy}:test`, getStr(trySize)); 16 count++; 17 } 18 } catch (err) { 19 return test(pieces, trySize - piece); 20 } 21 } else { 22 let K = size / 2 ** 10, 23 M = K / 2 ** 10; 24 return { K, M }; 25 } 26 } 27 28 _strategy.clear(); 29 30 const { K, M } = test( 31 [20, 18, 16, 13, 10].map((level) => 2 ** level), // 预估一个存储梯度 32 0, 33 ); 34 35 _strategy.clear(); 36 37 console.log(`${strategy}大致可存储 ${K}K,合约 ${M}M`); 38 } 39 40 env("localStorage"); 41 env("sessionStorage");
标签:10,存储,storage,js,strategy,trySize,test,pieces,size From: https://www.cnblogs.com/aurora-power/p/17416329.html