Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==UserScript==
// @name GM_[gs]etValue Bench
// @namespace https://github.com/arantius
// @description Test performance of lots of get/set value calls.
// @include http:*
// @version 1
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// ==/UserScript==
/*
Sample observed run:
set * 10 = 332.18 ms total, 33.22 ms each
get * 10 = 4.46 ms total, 0.45 ms each
mixed * 10 = 360.3 ms total, 36.03 ms each
set * 50 = 1559.29 ms total, 31.19 ms each
get * 50 = 16.83 ms total, 0.34 ms each
mixed * 50 = 1539.84 ms total, 30.8 ms each
set * 100 = 3349.78 ms total, 33.5 ms each
get * 100 = 62.31 ms total, 0.62 ms each
mixed * 100 = 3811.02 ms total, 38.11 ms each
*/
var r = [];
for (var i = 0; i < 217; i++); {
r.push(new String(Math.random()));
}
var ri = 0;
function rnd(num) {
return Math.round(100 * num) / 100;
}
function measure(name, measure, n) {
performance.clearMarks();
performance.clearMeasures();
performance.mark(name + '_start');
for (var i = 0; i < n; i++) {
measure('key0', r[ri++%217]);
measure('key1', r[ri++%217]);
measure('key2', r[ri++%217]);
measure('key3', r[ri++%217]);
measure('key4', r[ri++%217]);
measure('key5', r[ri++%217]);
measure('key6', r[ri++%217]);
measure('key7', r[ri++%217]);
measure('key8', r[ri++%217]);
measure('key9', r[ri++%217]);
}
performance.mark(name + '_finish');
performance.measure(name, name + '_start', name + '_finish');
var entrySet = performance.getEntriesByName(name)[0];
console.log(
name, '*', n*10, '=',
rnd(entrySet.duration), 'ms total,',
rnd(entrySet.duration / (n*10)), 'ms each');
}
function getset(a, b) {
GM_getValue(a);
GM_setValue(a, b);
}
function getsetdel(a, b) {
GM_getValue(a);
GM_setValue(a, b);
GM_deleteValue(a);
}
[1, 5, 10].forEach(function(n) {
measure('set', GM_setValue, n);
measure('get', GM_getValue, n);
measure('getset', getset, n);
measure('getsetdel', getsetdel, n);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.