하나의 이벤트 발생시 엄청나게 많은 연산이 필요할 때

이벤트가 계속 적으로 실행이되면 (예를 들어 mousemove 같은 지속적인 이벤트)

cpu 연산에 치명적이다.

그리고 연산이 많은 만큼 gc 의 대상도 많아진다.

event 가 실행되는 시점을 줄이는 것도 방법이 될 수 있다.

예를 들어

매초 10번 실행되는 이벤트를

매초 2번만 실행되는 이벤트로 바꿀 수 있다면

전체 cpu 연산을 1/5 로 줄일 수 있다.

$('.dom').on('event', 'selector', callback, 500  /* debounce time */);

this.addEvent('body', 'event', function () { }, 500);

this.addEvent('body', 'event', debounce(function() { }, this, 500));
function debounce (callback, context, delayed) {
  var timeId;
  return function () {
    if (timeId) clearTimeout(timeId);
    timdId = setTimeout(function() { 
      callback.call(context);
    }, delayed);
  }
}

results matching ""

    No results matching ""