🔍 inp.works

무거운 이벤트 핸들러: 클릭이 500ms 걸리는 이유와 해결 방법

무슨 일이 일어나는가

사용자가 클릭하면 브라우저는 다음 프레임을 그리기 전에 이벤트 핸들러를 끝까지 실행합니다. 핸들러가 수백 ms의 동기 작업 — 큰 배열 필터링, DOM 구축, JSON 파싱 — 을 하면 페이지는 정확히 그만큼 멈춥니다. 핸들러가 반환될 때까지 사용자에게는 아무 일도 일어나지 않는 것처럼 보입니다.

이는 가장 흔한 INP 문제이며, INP 분해에서 긴 processing time 단계로 나타납니다.

알아보는 법

해결 방법

문제

button.addEventListener("click", () => {
  const results = filterRows(allRows)   // 400 ms of sync work
  renderTable(results)                  // user saw nothing until now
})

해결

button.addEventListener("click", async () => {
  button.classList.add("loading")       // instant feedback, painted first
  await scheduler.yield()               // let the browser paint
  const results = filterRows(allRows)
  renderTable(results)
  button.classList.remove("loading")
})
사이트 무료로 스캔하기 →
Advertise here