Skip to content

Instantly share code, notes, and snippets.

@bahorn
Last active May 27, 2020 20:55
Show Gist options
  • Save bahorn/9414ad4433fb0456aa0ff1352e99e66a to your computer and use it in GitHub Desktop.
Save bahorn/9414ad4433fb0456aa0ff1352e99e66a to your computer and use it in GitHub Desktop.
turns out having devtools open makes this slower
<script>
const count = 500;
function timeFunction(fun, arg) {
var start = new Date().getTime();
fun(arg);
var end = new Date().getTime();
return (end - start);
}
function pure_compute() {
let v = 0;
console.clear();
for ( j = 0; j < count*1000; j++) {
v = (v+j) % 1024;
}
console.clear();
}
function console_check(fn) {
console.clear();
for (j = 0; j < count; j++) {
try {
console.assert(fn(j), true);
} catch (err) {
continue;
}
}
console.clear()
}
function sum(a, b) {
return a + b;
}
function experiment() {
var none_avg = [];
var all_avg = [];
var experiment_count = 3;
for (i = 0; i <= experiment_count; i++) {
none_avg.push(timeFunction(
pure_compute,
false
));
all_avg.push(timeFunction(
console_check,
() => false
));
}
console.log(none_avg, all_avg)
none_avg = none_avg.slice(1).reduce(sum, 0) / experiment_count;
all_avg = all_avg.slice(1).reduce(sum, 0) / experiment_count;
return [none_avg, all_avg];
}
function devtools_open(threshold) {
let resp = experiment();
return (resp[1]/resp[0]) > threshold;
}
function isDevtoolsOpen() {
if (devtools_open(20)) {
document.getElementById('output').innerHTML = 'Devtools Open';
} else {
document.getElementById('output').innerHTML = 'Devtools are not Open';
}
}
function main() {
const interval = timeFunction(isDevtoolsOpen)*10;
setInterval(isDevtoolsOpen, interval);
}
</script>
<body onload="main()">
<ul id="output">
</ul>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment