Javascript VS jQuery Find Scroll Direction

Benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Test runner

Ready to run.

Testing in
TestOps/sec
Javascript Solution
var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop,
  position = document.body.scrollTop;

function scrollD() {
  var scroll = document.body.scrollTop;

  if (scroll > position) {
    // scroll Down
  } else {
    // scrolling Up
  }
  position = scroll;
}
ready
jQuery Solution
var position = $(window).scrollTop();

$(window).scroll(function() {
  var scroll = $(window).scrollTop();
  if (scroll > position) {
    // scrolling downwards
  } else {
    // scrolling upwards
  }
  position = scroll;
});
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.