Created
April 20, 2020 10:26
-
-
Save victor-homyakov/5587d4baa9d9c16c68611b4e4fbf61ca to your computer and use it in GitHub Desktop.
Measure page scroll speed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Measure page scroll speed | |
(function() { | |
if (document.body.scrollHeight <= window.innerHeight) { | |
console.log('Scrolling measurement is only possible if the window can actually be scrolled!'); | |
return; | |
} | |
function calcScrollTime(startTime, iterations) { | |
return Math.round((Date.now() - startTime) / iterations) / 1000; | |
} | |
var MAX_ITERATIONS = 3, | |
startTime = Date.now(), | |
iterationStartTime = startTime, | |
iteration = MAX_ITERATIONS, | |
y = 0, | |
step = 10, | |
timer = setInterval(function() { | |
y += step; | |
if (y < document.body.scrollHeight && y >= 0) { | |
window.scrollTo(0, y); | |
} else if (--iteration) { | |
console.log('Scroll ' + (MAX_ITERATIONS - iteration) + ': ' + calcScrollTime(iterationStartTime, 1) + 's'); | |
iterationStartTime = Date.now(); | |
// scroll in reverse direction | |
step = -step; | |
} else { | |
clearInterval(timer); | |
console.log('It takes approximately ' + calcScrollTime(startTime, 3) + ' seconds to scroll this document'); | |
} | |
}, 0); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment