[Observer API νŒŒν—€μΉ˜κΈ°] 5. PerformanceObserver

⏰ 2024-06-27 (λͺ©) 11:27:14

screener
μ‹œλ¦¬μ¦ˆ λͺ¨μ•„보기
Observer API νŒŒν—€μΉ˜κΈ°

5 / 6

Table of Contents

  • 1. PerformanceObserver












PerformanceObserver

PerformanceObserverλŠ” μ„±λŠ₯ 이벀트λ₯Ό κ΄€μΈ‘ν•˜λŠ” μ˜΅μ €λ²„ APIλ‹€. F12λ₯Ό λˆ„λ₯΄λ©΄ λ‚˜μ˜€λŠ” κ°œλ°œμžλ„κ΅¬μ˜ μ„±λŠ₯ νƒ­κ³Ό μ—°κ΄€λœλ‹€.

TYPESCRIPT

1
2
3
const po = new PerformanceObserver(callback);

po.observer(options);

νŽ˜μ΄μ§€ μ„±λŠ₯κ³Ό κ΄€λ ¨λœ μ˜΅μ €λ²„λΌλŠ” νŠΉμ§•μœΌλ‘œμΈν•΄, 타 μ˜΅μ €λ²„μ™€ 달리 νƒœκ·Έλ₯Ό λ“±λ‘ν•˜λŠ” 과정이 μ—†λ‹€.

options

μ˜΅μ €λ²„ 등둝 μ‹œ optionsλ₯Ό μ „λ‹¬ν•˜μ—¬ μ„ΈλΆ€ μ˜΅μ…˜μ„ μ‘°μ •ν•  수 μžˆλ‹€. PerformanceObserverInit νƒ€μž…μ„ 가진닀.

κ΄€μ°° λŒ€μƒ (type)

μ˜΅μ €λ²„κ°€ κ΄€μ°°ν•  μ„±λŠ₯ μš”μ†Œλ₯Ό μ§€μ •ν•œλ‹€. 각 μš”μ†ŒλŠ” μ•„λž˜μ™€ κ°™λ‹€.

  1. navigation - νŽ˜μ΄μ§€ λ‘œλ“œμ™€ μ—°κ΄€λœ μ„±λŠ₯ μ§€ν‘œ. νŽ˜μ΄μ§€ 첫 λ‘œλ“œ μ‹œμ˜ 타이밍 정보 등을 μˆ˜μ§‘ν•  수 μžˆλ‹€.
  2. resource - νŽ˜μ΄μ§€μ— λ‘œλ“œλœ λ¦¬μ†ŒμŠ€μ™€ μ—°κ΄€λœ μ„±λŠ₯ μ§€ν‘œ. 이미지, js, css 등이 λŒ€μƒμ— ν•΄λ‹Ήλœλ‹€.
  3. mark - κ°œλ°œμžκ°€ μ§€μ •ν•œ μž„μ˜μ˜ 타이밍 정보.
  4. measure - μ§€μ •ν•œ 두 mark μ‚¬μ΄μ˜ μ‹œκ°„ μΈ‘μ •.
  5. paint - νŽ˜μ΄μ§€μ˜ λ Œλ”λ§ 및 νŽ˜μΈνŠΈμ™€ κ΄€λ ¨λœ μ„±λŠ₯ μ§€ν‘œ. 첫 νŽ˜μ΄μ§€ νŽ˜μΈνŒ…(FP), 졜초의 μœ μ˜λ―Έν•œ νŽ˜μΈνŒ…(FMP) 등이 ν•΄λ‹Ήν•œλ‹€.
  6. longtask - 50ms μ΄μƒμ˜ κΈ΄ ν”„λ‘œμ„ΈμŠ€ 식별
  7. element - DOM μš”μ†Œ λ Œλ”λ§κ³Ό μ—°κ΄€λœ μ„±λŠ₯ μ§€ν‘œ.
  8. frame - μ• λ‹ˆλ©”μ΄μ…˜ ν”„λ ˆμž„κ³Ό λ ¨κ΄€λœ μ„±λŠ₯ μ§€ν‘œ. μ• λ‹ˆλ©”μ΄μ…˜ 및 슀크둀 μ„±λŠ₯ 뢄석과 κ΄€λ ¨λœ 정보λ₯Ό μ œκ³΅ν•œλ‹€.
  9. event - 이벀트 λ°œμƒκ³Ό μ—°κ΄€λœ μ„±λŠ₯ μ§€ν‘œ.
  10. first-input - μ‚¬μš©μžμ˜ μ•‘μ…˜κ³Ό κ΄€λ ¨λœ μ„±λŠ₯ μ§€ν‘œ.
  11. largest-contentful-paint - λŒ€μš©λŸ‰ μš”μ†Œμ˜ λ Œλ”λ§ μ„±λŠ₯ μ§€ν‘œ.
  12. layout-shift - νŽ˜μ΄μ§€μ˜ μš”μ†Œ 이동 μ„±λŠ₯ μ§€ν‘œ.

type은 ν›„μˆ ν•  entryTypes와 λ³‘ν–‰ν•˜μ—¬ μ‚¬μš©ν•  수 μ—†λ‹€.

κ΄€μ°° λŒ€μƒ λͺ©λ‘ (entryTypes)

μ˜΅μ €λ²„κ°€ κ΄€μ°°ν•  μ„±λŠ₯ μš”μ†Œλ₯Ό λ°°μ—΄λ‘œ μ§€μ •ν•œλ‹€. μ—¬λŸ¬ μ„±λŠ₯ μ§€ν‘œλ₯Ό μΈ‘μ •ν•  λ•Œ μš©μ΄ν•˜λ‹€.

κΈ°μ‘΄ μ„±λŠ₯ ν•­λͺ© 포함 μ—¬λΆ€ (buffered)

κΈ°μ‘΄ μ„±λŠ₯ μ§€ν‘œ 포함 μ—¬λΆ€λ₯Ό μ§€μ •ν•˜λŠ” μ˜΅μ…˜μ΄λ‹€. 기본적으둜 false둜 μ„ μ–Έλ˜μ–΄μžˆλ‹€.

callback

콜백 λ©”μ„œλ“œλ₯Ό 톡해 μ„±λŠ₯ κ΄€λ ¨ μ§€ν‘œλ₯Ό λ‹€λ£° 수 μžˆλ‹€. PerformanceObserverCallback νƒ€μž…μ„ 가진닀.

entries

μ„±λŠ₯ μ§€ν‘œ 객체 PerformanceObserverEntryListλ₯Ό λ°˜ν™˜ν•œλ‹€. getEntries, getEntriesByName, getEntriesByType λ©”μ„œλ“œλ₯Ό 톡해 μ›ν•˜λŠ” λͺ©λ‘μ„ 뽑아낼 수 μžˆλ‹€.

각 μš”μ†ŒλŠ” type ν˜Ήμ€ entryTypes에 μ§€μ •ν•œ μš”μ†Œμ— λ”°λ₯Έ μ„±λŠ₯ κ°œμ²΄λ‹€. μΈ‘μ •λœ μ„±λŠ₯ μš”μ†Œμ— 따라 μƒμ΄ν•œ 객체λ₯Ό 가지고 μžˆλ‹€.

observer

PerformanceObserver 객체λ₯Ό λ°˜ν™˜ν•΄μ€€λ‹€. 이λ₯Ό 톡해 콜백 λ©”μ„œλ“œ λ‚΄μ—μ„œλ„ PerformanceObserverλ₯Ό μ—°μ‡„μ μœΌλ‘œ λ‹€λ£° 수 μžˆλ‹€.

Reactμ—μ„œ μ»€μŠ€ν…€ ν›…μœΌλ‘œ κ°„νŽΈν•˜κ²Œ μ‚¬μš©ν•˜κΈ°

PerformanceObserverλ₯Ό μ»€μŠ€ν…€ 훅을 톡해 κ°„νŽΈν•˜κ²Œ μ‚¬μš©ν•΄λ³΄μž.

TYPESCRIPT

1
2
3
4
export function usePerformanceObserver(): void
{
  //
}

μœ„μ™€ 같이 usePerformanceObserver λ©”μ„œλ“œλ₯Ό μ •μ˜ν•œλ‹€.

useMutationObserverλ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„  μ•„λž˜μ˜ 두 μš”μ†Œκ°€ ν•„μš”ν•˜λ‹€.

  1. 콜백 λ©”μ„œλ“œ
  2. μ˜΅μ…˜

μ§€κΈˆκΉŒμ§€μ˜ μ˜΅μ €λ²„μ™€ 달리 DOM이 ν•„μš”ν•˜μ§€ μ•Šλ‹€.

TYPESCRIPT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { useEffect } from "react";

export type UsePerformanceObserverCallback = (entry: PerformanceEntry) => void;

/**
 * PerformanceObserver 적용 ν›… λ©”μ„œλ“œ
 *
 * @param {UsePerformanceObserverCallback} callback: 콜백 λ©”μ„œλ“œ
 * @param {PerformanceObserverInit} options: μ˜΅μ…˜
 */
export function usePerformanceObserver(
  callback: UsePerformanceObserverCallback,
  options?: PerformanceObserverInit
): void {
  useEffect(() => {
    const po = new PerformanceObserver((entries) => {
      entries.getEntries().forEach(callback);
    });
  }, [callback, options]);
}

PerformanceObserverλ₯Ό μ΄ˆκΈ°ν™”ν•˜μ—¬ po λ³€μˆ˜λ‘œ ν• λ‹Ήν•œλ‹€.

po.observe() λ©”μ„œλ“œλ₯Ό 톡해 νƒœκ·Έλ₯Ό 등둝할 수 μžˆλ‹€.

TYPESCRIPT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { useEffect } from "react";

export type UsePerformanceObserverCallback = (entry: PerformanceEntry) => void;

/**
 * PerformanceObserver 적용 ν›… λ©”μ„œλ“œ
 *
 * @param {UsePerformanceObserverCallback} callback: 콜백 λ©”μ„œλ“œ
 * @param {PerformanceObserverInit} options: μ˜΅μ…˜
 */
export function usePerformanceObserver(
  callback: UsePerformanceObserverCallback,
  options?: PerformanceObserverInit
): void {
  useEffect(() => {
    const po = new PerformanceObserver((entries) => {
      entries.getEntries().forEach(callback);
    });

    po.observe(options);
  }, [callback, options]);
}

λ§ˆμ§€λ§‰μœΌλ‘œ, μ»΄ν¬λ„ŒνŠΈκ°€ λ Œλ”λ§ 될 λ•Œλ§ˆλ‹€ PerformanceObserver의 등둝이 μ€‘μ²©λ˜μ§€ μ•Šλ„λ‘, μ΄ˆκΈ°ν™” μ½”λ“œλ₯Ό λ„£μ–΄μ€€λ‹€.

po.disconnect() λ©”μ„œλ“œλ₯Ό 톡해 PerformanceObserverλ₯Ό μ œκ±°ν•  수 μžˆλ‹€.

전체 μ½”λ“œ

TYPESCRIPT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { useEffect } from "react";

export type UsePerformanceObserverCallback = (entry: PerformanceEntry) => void;

/**
 * PerformanceObserver 적용 ν›… λ©”μ„œλ“œ
 *
 * @param {UsePerformanceObserverCallback} callback: 콜백 λ©”μ„œλ“œ
 * @param {PerformanceObserverInit} options: μ˜΅μ…˜
 */
export function usePerformanceObserver(
  callback: UsePerformanceObserverCallback,
  options?: PerformanceObserverInit
): void {
  useEffect(() => {
    const po = new PerformanceObserver((entries) => {
      entries.getEntries().forEach(callback);
    });

    po.observe(options);

    return () => {
      po.disconnect();
    };
  }, [callback, options]);
}

전체 μ½”λ“œλŠ” μœ„μ™€ κ°™λ‹€.

CodeSandbox둜 ν†΅ν•œ μ˜ˆμ‹œ

CodeSandbox둜 κ°„λ‹¨ν•œ μ˜ˆμ‹œλ₯Ό κ΅¬ν˜„ν–ˆλ‹€.

마치며

Lighthouse μ„±λŠ₯ μΈ‘μ •

PerformanceObserverλ₯Ό 톡해 νŽ˜μ΄μ§€μ˜ μ„±λŠ₯ μ§€ν‘œλ₯Ό μΈ‘μ •ν•  수 μžˆλ‹€. 이λ₯Ό 적절히 κ΅¬μ„±ν•˜λ©΄ Lighthouse와 같은 μ›Ή μ„±λŠ₯ μΈ‘μ • μ„œλΉ„μŠ€λ₯Ό μœ μ‚¬ν•˜κ²Œ κ΅¬ν˜„ν•  수 μžˆμ„ 것이닀.


ν•˜μ§€λ§Œ 이미 λΉ„μŠ·ν•˜κ±°λ‚˜, 더 μš°μˆ˜ν•œ κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ” μ—¬λŸ¬ μ˜€ν”ˆ/μƒμš© 도ꡬ듀이 λ§Žμ€ 것이 ν˜„μ‹€μ΄λ‹€. λ˜ν•œ μ•žμ„œ 닀룬 μ„Έ μ˜΅μ €λ²„μ™€ 달리, κΈ°λŠ₯ κ°œλ°œμ— μžˆμ–΄ λ²”μš©μ„±μ΄ 많이 λ–¨μ–΄μ§€λŠ” 탓에, κ΄€λ ¨ 정보가 많이 μ—†λ‹€λŠ” 것이 단점이닀.

λ§ˆμ§€λ§‰μœΌλ‘œ λ‹€λ£° μ˜΅μ €λ²„λŠ” ReportingObserverλ‹€.


🏷️ νƒœκ·Έ
# JavaScript
# TypeScript
# React
# Observer API
# PerformanceObserver

μ½μ–΄μ£Όμ…”μ„œ κ³ λ§ˆμ›Œμš”!

도움이 λ˜μ…¨λ‹€λ©΄, κ³΅κ°μ΄λ‚˜ λŒ“κΈ€μ„ λ‹¬μ•„μ£Όμ‹œλŠ” 건 μ–΄λ–€κ°€μš”?

λΈ”λ‘œκ·Έ μš΄μ˜μ— 큰 힘이 λ©λ‹ˆλ‹€.

https://hits.seeyoufarm.com/api/count/incr/badge.svg?count_bg=%23484848&icon=react.svg&icon_color=dodgerblue&title=view&title_bg=%23242424&url=https%3A%2F%2Fblog.itcode.dev%2Fposts%2F2024%2F06%2F27%2Fobserver-api-5