Javascript VS jQuery Find Scroll Direction (v7)

Revision 7 of this benchmark created on


Preparation HTML

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

Test runner

Ready to run.

Testing in
TestOps/sec
Javascript Solution
var lastST = 0,
  ST,
  direction;

function detectDirection() {
  ST = window.pageYOffset;
  if (ST > lastST) {
    // down
  } else if (ST < lastST) {
    // up
  } else {
    // static
  }
  lastST = ST;
}

$(window).bind('scroll', function() {
  detectDirection();
});
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.