I'm trying to find confirmation if I should prefer to use .forEach instead of .subscribe when I want to listen in on Angular's FormControl.valueChanges observable. FormControl API
In Angular's Documentation for forms they use .forEach without really explaining why and say you don't really need to understand it. Document Link
I also know from this answer that .forEach will bundle a sort of finite duration of events/incoming values into a promise.
So I tried to test both .forEach and .subscribe myself thinking that .forEach will be more performant in bundling keyboard input events if the user were to type monkey crazy on it.
constructor(private fb: FormBuilder) {
this.createForm();
}
private createForm() {
this.myForm = this.fb.group({
name: '',
});
}
private forEach = this.myForm.get('name').valueChanges.forEach(value => {
console.log(value, 'forEach');
});
private subscription = this.myForm.get('name').valueChanges.subscribe(value => {
console.log(value, 'subscribe');
});
In my test manually typing fast in the html input did not bundle .forEach values as I expected. Instead both .subscribe and .forEach emited values one character at a time.
Is there any benefit to .forEach over .subscribe in this situation and how can I create a test that can show the difference if there is a benefit to using .forEach over .subscribe?