Angular 2+ でデバウンス:パフォーマンスとユーザーインターフェースの向上
デバウンスとは?
Angular 2+ でデバウンスを使う
Angular 2+ では、RxJS
ライブラリを使ってデバウンスを実装することができます。RxJS
には debounce()
というオペレータがあり、イベントストリームをデバウンスすることができます。
例
import { Observable } from 'rxjs';
// イベントストリーム
const inputStream = Observable.fromEvent(document.getElementById('input'), 'input');
// デバウンス処理
const debouncedStream = inputStream.pipe(
debounce(() => Observable.timer(500))
);
// デバウンスされたストリームを処理
debouncedStream.subscribe((event) => {
// 500ms 間隔で処理される
console.log(event.target.value);
});
上記の例では、input
イベントをデバウンスし、500ms 間隔で処理されるようにしています。
デバウンスを使うメリット
- 無駄な処理を減らすことができる
- パフォーマンスを向上させることができる
- ユーザーインターフェースを滑らかにする
デバウンスを使うデメリット
- 処理に遅延が発生する
- リアルタイム性が損なわれる
デバウンスは、イベント処理を効率化するために有効なテクニックです。ただし、デメリットも理解した上で使用する必要があります。
debounce() オペレータを使う
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
inputStream: Observable<any>;
debouncedStream: Observable<any>;
constructor() {
this.inputStream = Observable.fromEvent(document.getElementById('input'), 'input');
}
ngOnInit() {
// デバウンス処理
this.debouncedStream = this.inputStream.pipe(
debounce(() => Observable.timer(500))
);
// デバウンスされたストリームを処理
this.debouncedStream.subscribe((event) => {
// 500ms 間隔で処理される
console.log(event.target.value);
});
}
}
debounceTime() オペレータを使う
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
inputStream: Observable<any>;
debouncedStream: Observable<any>;
constructor() {
this.inputStream = Observable.fromEvent(document.getElementById('input'), 'input');
}
ngOnInit() {
// デバウンス処理
this.debouncedStream = this.inputStream.pipe(
debounceTime(500)
);
// デバウンスされたストリームを処理
this.debouncedStream.subscribe((event) => {
// 500ms 間隔で処理される
console.log(event.target.value);
});
}
}
debounceUntil() オペレータを使う
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
inputStream: Observable<any>;
debouncedStream: Observable<any>;
constructor() {
this.inputStream = Observable.fromEvent(document.getElementById('input'), 'input');
}
ngOnInit() {
// デバウンス処理
this.debouncedStream = this.inputStream.pipe(
debounceUntil(() => Observable.timer(500))
);
// デバウンスされたストリームを処理
this.debouncedStream.subscribe((event) => {
// 500ms 間隔で処理される
console.log(event.target.value);
});
}
}
debounce ディレクティブを使う
<input type="text" [(ngModel)]="value" (input)="onInput($event)" debounce="500">
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
value: string;
constructor() {}
ngOnInit() {}
onInput(event: any) {
// 500ms 間隔で処理される
console.log(event.target.value);
}
}
上記のサンプルコードは、いずれも `
Angular 2+ でデバウンスを実装するその他の方法
Lodash を使う
import _ from 'lodash';
const debouncedFunction = _.debounce((event) => {
// 処理
}, 500);
// イベント発生時に処理を実行
document.getElementById('input').addEventListener('input', debouncedFunction);
Underscore.js を使う
import _ from 'underscore';
const debouncedFunction = _.debounce((event) => {
// 処理
}, 500);
// イベント発生時に処理を実行
document.getElementById('input').addEventListener('input', debouncedFunction);
RxJS の throttle() オペレータを使う
import { Observable } from 'rxjs';
const inputStream = Observable.fromEvent(document.getElementById('input'), 'input');
// スロットル処理
const throttledStream = inputStream.pipe(
throttle(() => Observable.timer(500))
);
// スロットルされたストリームを処理
throttledStream.subscribe((event) => {
// 500ms 間隔で処理される
console.log(event.target.value);
});
RxJS の throttleTime() オペレータを使う
import { Observable } from 'rxjs';
const inputStream = Observable.fromEvent(document.getElementById('input'), 'input');
// スロットル処理
const throttledStream = inputStream.pipe(
throttleTime(500)
);
// スロットルされたストリームを処理
throttledStream.subscribe((event) => {
// 500ms 間隔で処理される
console.log(event.target.value);
});
RxJS の sample() オペレータを使う
import { Observable } from 'rxjs';
const inputStream = Observable.fromEvent(document.getElementById('input'), 'input');
// サンプル処理
const sampledStream = inputStream.pipe(
sample(Observable.timer(500))
);
// サンプルされたストリームを処理
sampledStream.subscribe((event) => {
// 500ms 間隔で処理される
console.log(event.target.value);
});
デバウンスは、イベント処理を効率化するために有効なテクニックです。Angular 2+ でデバウンスを実装するには、さまざまな方法があります。
javascript angular typescript