Forked from victor-homyakov/measure-page-scroll-speed.js
Created
May 30, 2020 19:55
-
-
Save designervoid/f243ebd515f346618fadc82681b29d15 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