油猴 Tampermonkey 中 js 的数组、对象、map、set 存储数据效率对比
数组存对象和 Set 集合存对象:
const thrSet = new Set();
let arr = [];
let i = 0;
while (i < 10000) {
const thread = {
tid: 1,
touseruid: 1,
touser: 1,
replyIndex: 1,
replyLen: 1,
randomTime: 1,
}
thrSet.add(thread);
arr.push(thread);
i++;
}
let setarr = [...thrSet]; // 相当于整个数组遍历一遍,增加时间
console.time('setSet');
GM_setValue("thrSet", setarr);
console.timeEnd('setSet');
console.time('setArr');
GM_setValue("arr", arr);
console.timeEnd('setArr');
console.time('getSet');
let set = GM_getValue("thrSet");
console.timeEnd('getSet');
console.time('getArr');
let ar = GM_getValue("arr");
console.timeEnd('getArr');
setset:14.48185546875ms
setarr: 11. 010986328125ms
getset:4.697821484375ms
staRrr:4.72599765625ms
差距很小
数组存数组和 Set 集合存数组:
const thrSet = new Set();
let arr = [];
let i = 0;
while (i < 10000) {
const thread = {
tid: 1,
touseruid: 1,
touser: 1,
replyIndex: 1,
replyLen: 1,
randomTime: 1,
}
thrSet.add([1, 1, 1, 1, 1]);
arr.push([1, 1, 1, 1, 1]);
i++;
}
let setarr = [...thrSet]; // 相当于整个数组遍历一遍,增加时间
console.time('setSet');
GM_setValue("thrSet", setarr);
console.timeEnd('setSet');
console.time('setArr');
GM_setValue("arr", arr);
console.timeEnd('setArr');
console.time('getSet');
let se = GM_getValue("thrSet");
console.timeEnd('getSet');
console.time('getArr');
let ar = GM_getValue("arr");
console.timeEnd('getArr');
setset:2.466964453125ms
setAr:4.576171875ms
getset:2.852978515625ms
retArd:2.85498846875ms
第二次测试:
setset:3.65673828125ms
setAr;4.427978515625ms
getset:2.888916015625ms
staRr:3.433837898625ms
差距很小
数组存数组和 Map 映射存数组:
const thrSet = new Map();
let arr = [];
let i = 0;
while (i < 10000) {
const thread = {
tid: 1,
touseruid: 1,
touser: 1,
replyIndex: 1,
replyLen: 1,
randomTime: 1,
}
thrSet.set(i, [1, 1, 1, 1, 1]);
arr.push([1, 1, 1, 1, 1]);
i++;
}
let setarr = [...thrSet]; // 相当于整个数组遍历一遍,增加时间
console.time('setMap');
GM_setValue("thrSet", setarr);
console.timeEnd('setMap');
console.time('setArr');
GM_setValue("arr", arr);
console.timeEnd('setArr');
console.time('getMap');
let se = GM_getValue("thrSet");
console.timeEnd('getMap');
console.time('getArr');
let ar = GM_getValue("arr");
console.timeEnd('getArr');
strAp:4.163818359375ms
setAr:4.93693515625ms
getMap:4.588878125ms
staRr:1.696244921875ms
第二次测试
setMap: 3.80078125ms
setAr:4.868115234375ms
getMap:3.119873846875ms
staRr:3.635986328125ms
第三次测试
setap:4.934814453125ms
setAr:3.2888859375ms
getMap:3.6669921875ms
staRr:2.56298828125ms
差距很小
数组存对象和 Map 映射存对象:
function launch1() {
const thrSet = new Map();
let arr = [];
let i = 0;
while (i < 10000) {
const thread = {
tid: 1,
touseruid: 1,
touser: 1,
replyIndex: 1,
replyLen: 1,
randomTime: 1,
}
thrSet.set(thread);
arr.push(thread);
i++;
}
console.time('setthrSet');
let setarr = [...thrSet]; // 相当于整个数组遍历一遍,增加时间
console.timeEnd('setthrSet');
console.time('setMap');
GM_setValue("thrSet", setarr);
console.timeEnd('setMap');
console.time('setArr');
GM_setValue("arr", arr);
console.timeEnd('setArr');
console.time('getMap');
let se = GM_getValue("thrSet");
console.timeEnd('getMap');
console.time('getArr');
let ar = GM_getValue("arr");
console.timeEnd('getArr');
}
function launch2() {
}
function swRePic() {
console.log("kkk");
}
setthrSet: 0.6669921875 ms
strAp:12.662841796875ms
setAr:11.68382734375ms
getmap:6.895947265625ms
setAr:3.727783283125ms
第二次测试
setthrSet: 0.6337890625 ms
strAp:15.3818359375ms
setAr:17.834228515625ms
getMap: 7.79296875ms
staRr:6.92587899625ms
差距很小
油猴脚本中,数组存储比对象高效吗?
数组存数组和 Set 集合存对象:
const thrSet = new Set();
let arr = [];
let i = 0;
while (i < 10000) {
const thread = {
tid: 1,
touseruid: 1,
touser: 1,
replyIndex: 1,
replyLen: 1,
randomTime: 1,
}
thrSet.add(thread);
arr.push([1, 1, 1, 1, 1]);
i++;
}
let setarr = [...thrSet]; // 相当于整个数组遍历一遍,增加时间
console.time('setSet');
GM_setValue("thrSet", setarr);
console.timeEnd('setSet');
console.time('setArr');
GM_setValue("arr", arr);
console.timeEnd('setArr');
console.time('getSet');
let se = GM_getValue("thrSet");
console.timeEnd('getSet');
console.time('getArr');
let ar = GM_getValue("arr");
console.timeEnd('getArr');
用数组代替对象?
setset:13.416015625ms
setarr:2.85791815625ms
getset:8.629158398625ms
getArr:1.35595793125ms
数组写入快四倍!
数组读取快六倍!
应该使用数组或集合,只有键的集合比键值对快很多
第二次测试
setset:11.566162189375ms
setar:2.989998234375ms
getset:5.27587898625ms
setAr:5.89111328125ms
第三次测试
setset:10.49966796875ms
setAr:2.97999399625ms
getset:4.8869149625ms
staRr:4.846923828125ms
第四次测试
setset:17.333984375ms
setAr:3.989765625ms
getset:14.43701171875ms
staRr:6.9951171875ms
第五次测试
reset:14.884833283125ms
setAr:2.69138859375ms
getset:5.759933293125ms
staRr:1.711181649625ms
第六次测试
setset:18.80878125ms
setAr:2.327888859375ms
getset:4.826968359375ms
staRr:1.85187421875ms
第七次测试
setset:18.899987899625ms
setAr:2.93817578125ms
getset:4.4268984375ms
staRr:2.72599765625ms