JavaScript and RxJS Password Strength Indicator This is my first time using RxJS (or any observable library). It seems to work okay but I'm not sure about best practices.

The thing that I am most unsure about is subscribing to the observable multiple times. I couldn't decide if it would be better to subscribe to it once with one big function, or to use many smaller subscribers.

CodePen Demo

Markup

&lt;div class="field"&gt;
  &lt;label class="label"&gt;Password&lt;/label&gt;
  &lt;div class="control has-icons-left"&gt;
    &lt;input id="password" class="input" type="password" /&gt;
    &lt;span class="icon is-small is-left"&gt;
      &lt;i class="fa fa-lock"&gt;&lt;/i&gt;
    &lt;/span&gt;
  &lt;/div&gt;
  &lt;p id="passwordHelp" class="help is-danger"&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class="field"&gt;
  &lt;label class="label"&gt;Password Strength&lt;/label&gt;
  &lt;progress id="passwordStrength" class="progress is-danger" value="0" max="5" /&gt;
&lt;/div&gt;


Script

const { fromEvent } = rxjs
const { map, filter } = rxjs.operators

document.addEventListener('DOMContentLoaded', () =&gt; {
  const passwordInput = document.getElementById('password')
  const passwordHelp = document.getElementById('passwordHelp')
  const passwordStrength = document.getElementById('passwordStrength')

  const passwordChanged$ = fromEvent(passwordInput, 'input')
    .pipe(
      map(x =&gt; x.target.value),
      map(zxcvbn)
    )

  passwordChanged$
    .pipe(map(password =&gt; password.score &gt; 2 ? 'success' : 'danger'))
    .subscribe(state =&gt; {
      passwordInput.className = `input is-${state}`
      passwordStrength.className = `progress is-${state}`
    })

  passwordChanged$
    .subscribe(password =&gt; { passwordHelp.innerHTML = password.feedback.warning })

  passwordChanged$
    .subscribe(password =&gt; { passwordStrength.setAttribute('value', password.score + 1) })
})
Each subscribe(...) invocation results in creating and returning a new Subscription unless special measures are taken (like .publish(), .share()...)


  Subscribing to observables
  
  Remember, observables are lazy. If you don’t subscribe nothing is going to happen. It’s good to know that when you subscribe to an observer, each call of subscribe() will trigger it’s own independent execution for that given observer. Subscribe calls are not shared among multiple subscribers to the same observable. — Luuk Gruijs


Each Subscription


eats up a bit of resources;
is processed separately from the others;
and needs to be unsubscribed separately to release the resources.


Therefore, if you have no special reasons, you should subscribe the least number of times. In your case, a single subscription is sufficient. You can still use separate functions to delegate them the actual work (setClassNames(), setWarning(), setStrength()).



const { fromEvent } = rxjs
const { map, filter } = rxjs.operators

document.addEventListener('DOMContentLoaded', () =&gt; {
  const passwordInput = document.getElementById('password')
  const passwordHelp = document.getElementById('passwordHelp')
  const passwordStrength = document.getElementById('passwordStrength')

  const passwordChanged$ = fromEvent(passwordInput, 'input')
    .pipe(
      map(x =&gt; x.target.value),
      map(zxcvbn)
    )

  const setClassNames = password =&gt; {
    const state = password.score &gt; 2 ? 'success' : 'danger';
    passwordInput.className = `input is-${state}`
    passwordStrength.className = `progress is-${state}`
  };
  const setWarning = password =&gt; { passwordHelp.innerHTML = password.feedback.warning };
  const setStrength = password =&gt; { passwordStrength.setAttribute('value', password.score + 1) };

  passwordChanged$
    .subscribe(password =&gt; {
      setClassNames(password);
      setWarning(password);
      setStrength(password);
    });
})