** π 9λ¨κ³ λ¬Έμ λ€μ 보기**
100κ°μ μν μ 보λ₯Ό Ajaxλ‘ λΆλ¬μ€λλ°
μ¬μ©μκ° λΉ λ₯΄κ² μ€ν¬λ‘€νλ©΄μ κ³μ μμ²μ 보λΈλ€.
μλ²λ μ²λ¦¬ κ°λ₯ν νμ(TPS)κ° μ νλμ΄ μλ€.
λΉμ μ λͺ©νλ?
- β μλ² κ³ΌλΆν λ°©μ§
- β μ€λ³΅ μμ² μ κ±°
- β UI μλ΅μ± μ μ§
β ν΄κ²° μ λ΅ νλμ 보기
μ λ΅ | λͺ©μ |
---|---|
Debounce |
λ무 μμ£Ό μμ²νμ§ μλλ‘ λλ μ΄ μ€ |
μμ² ν |
μμλλ‘ μ°¨λ‘μ°¨λ‘ μμ²νκ² ν¨ |
μλ΅ μΊμ± |
μ΄λ―Έ λ°μ λ°μ΄ν°λ λ€μ μμ²νμ§ μμ |
μμ² μ·¨μ |
μ΄μ μ λ λ¦° μμ²μ΄ μμ§ μ λλ¬λ€λ©΄ μ·¨μν¨ |
β 1. Debounceλ‘ μμ² λΉλ μ‘°μ
π κ°λ
μ¬μ©μκ° λΉ λ₯΄κ² μ€ν¬λ‘€ν΄λ 0.3μ΄ λμ λ©μΆλ©΄ κ·Έλ 1λ²λ§ μμ²
π¦π» μ¬μ΄ μ€λͺ
βμλ§~ μλ§~ μλ§~ μλ§!β
β 0.3μ΄ λμ μ λΆλ₯΄λ©΄ κ·Έλ λ§ λ€μ΄μ£Όλ κ² π§
β μ½λ μμ
function debounce(fn, delay) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), delay);
};
}
const requestItems = debounce(() => {
fetchItems(); // Ajax νΈμΆ
}, 300);
β μ¬μ©μκ° μ€ν¬λ‘€ν λλ§λ€ νΈμΆν΄λ 300ms λ©μΆλ©΄ 1λ²λ§ μ€ν
β 2. μμ² νλ‘ κ³ΌλΆν λ°©μ§
π κ°λ
μλ²λ ν λ²μ λ§μ΄ λͺ» λ°μΌλ, μμ²μ μ€ μΈμμ νλμ© λ³΄λ΄μΌ ν¨
π¦π» μ¬μ΄ μ€λͺ
νλ²κ±° κ°κ²μμ νκΊΌλ²μ 50λͺ μ΄ λͺ°λ¦¬λ©΄ μ£Όλ¬Έ λͺ» λ°μ
β μ€ μΈμμ 1λͺ μ© μ²λ¦¬νλ©΄ λ¬Έμ μμ π
β μ½λ μμ
class AjaxQueue {
constructor() {
this.queue = [];
this.running = false;
}
enqueue(task) {
this.queue.push(task);
this.runNext();
}
async runNext() {
if (this.running || this.queue.length === 0) return;
this.running = true;
const task = this.queue.shift();
await task();
this.running = false;
this.runNext();
}
}
const queue = new AjaxQueue();
queue.enqueue(() => fetch("/api/products?page=1"));
β μμ²μ νλ λλ λκΉμ§ λ€μ μμ² λκΈ° β TPS 보νΈ!
β 3. μλ΅ μΊμ±μΌλ‘ μ€λ³΅ μ κ±°
π κ°λ
μ΄λ―Έ λ°μμ¨ νμ΄μ§λ λ€μ μμ²νμ§ μκ³ μΊμμ μ μ₯ν κ±Έ κ·Έλλ‘ λ³΄μ¬μ€
π¦π» μ¬μ΄ μ€λͺ
λ§νΈμμ κ°μ 물건 λ μ¬λ¬ κ°λ €λλ°
βμ΄? μ§μ μλ€?β β λ€μ μ κ°λ λ¨ π
β μ½λ μμ
const cache = {};
function getProducts(page) {
if (cache[page]) return Promise.resolve(cache[page]);
return fetch(`/api/products?page=${page}`)
.then(res => res.json())
.then(data => {
cache[page] = data;
return data;
});
}
β λ€νΈμν¬ λλΉ μμ
β μλ² λΆλ΄ μ κ°
β μ¬μ©μ μ²΄κ° μλβ
β 4. μμ² μ·¨μλ‘ μ€λ³΅ μλ΅ λ°©μ§
π κ°λ
λΉ λ₯΄κ² μ°μ μμ²νλ©΄ μ΄μ μμ²μ΄ λλκΈ° μ μ μλ‘μ΄ μμ²μ 보λ΄μΌ ν¨
β μ΄λ μ΄μ μμ²μ abort() μ²λ¦¬
π¦π» μ¬μ΄ μ€λͺ
μλ§νν βνλ²κ±° μ¬μ!β νλλ°
1μ΄ λ€μ βμλ λ‘λ³Άμ΄!β β νλ²κ±°λ μ·¨μν΄μΌ ν¨
β μ½λ μμ
let controller;
function getPage(page) {
if (controller) controller.abort(); // μ΄μ μμ² μ·¨μ
controller = new AbortController();
return fetch(`/api/products?page=${page}`, {
signal: controller.signal
})
.then(res => res.json());
}
β UIμμ λ§μ§λ§ μμ²λ§ μ΄μλ¨κ² λ§λ€ μ μμ
β μλ²λ λ νλ¦
β μ΅μ’ μ λ΅ μμ½ (π μ€λ¬΄ μ μ©ν)
π§ μ¬μ©μκ° λΉ λ₯΄κ² μ€ν¬λ‘€ β
1. Debounceλ‘ μμ² λΉλ μ μ΄ β
2. μ΄λ―Έ λ³Έ νμ΄μ§λ μΊμλ‘ λ°ν β
3. μ μμ²μ Ajax Queueλ‘ μ€ μΈμ°κΈ° β
4. μ€κ°μ μ·¨μλλ©΄ AbortControllerλ‘ μ΄μ μμ² μ·¨μ
β λ©΄μ μ§λ¬Έ μμ + μ λ΅ κ΅¬μ‘°
β Q. μν λͺ©λ‘μ λΉ λ₯΄κ² μ€ν¬λ‘€νλ©΄μ Ajax μμ²μ΄ λ°λ³΅λ λ,
μλ² κ³ΌλΆν μμ΄ ν¨μ¨μ μΌλ‘ μλ΅μ±κ³Ό μ±λ₯μ μ μ§νλ €λ©΄ μ΄λ»κ² νμκ² μ΅λκΉ?
β A.
- μ¬μ©μμ μ€ν¬λ‘€ μ΄λ²€νΈλ₯Ό
Debounce
λ‘ μ μ΄ν΄ μμ² λΉλλ₯Ό μ€μ΄κ³ , - κ°μ λ°μ΄ν°λ₯Ό μ€λ³΅ μμ²νμ§ μλλ‘ μλ΅ μΊμλ₯Ό μ¬μ©νλ©°,
- μλ²μ TPS μ νμ κ³ λ €ν΄ Ajax μμ²μ νλ‘ μμ°¨ μ²λ¦¬νκ³ ,
-
λΉ λ₯΄κ² λ°μν μ΄μ μμ²μ
AbortController
λ‘ μ·¨μνμ¬μλ² λΆνμ UI κΌ¬μμ λ°©μ§ν©λλ€.
β μ 체 μμ½ μΉ΄λ
κΈ°μ | λͺ©μ | μ€λͺ |
---|---|---|
Debounce | λΉλ μ ν | μ°μ μ λ ₯μ λ¬Άμ΄μ ν λ²λ§ μ€ν |
Ajax Queue | κ³ΌλΆν λ°©μ§ | μμ²μ μμλλ‘ 1κ°μ© μ²λ¦¬ |
μλ΅ μΊμ± | μ€λ³΅ μ κ±° | μ΄λ―Έ λ°μ λ°μ΄ν°λ μ¬μ¬μ© |
μμ² μ·¨μ | κΌ¬μ λ°©μ§ | μ€κ° μμ²μ Abortλ‘ μ κ±° |