DEV Community

Cover image for RxJs vs Angular Signal performance
Kafeel Ahmad (kaf shekh)
Kafeel Ahmad (kaf shekh)

Posted on

RxJs vs Angular Signal performance

What we measure this time is simple: an event changes a value, and a child component gets that value. The time we record is right before setting the value, until the input changes in the child component. The framework's overhead can be removed from the equation as it ought to be the same, and also we want to measure an end-to-end scenario, from triggering a change in RxJs/Signal until we can use it. To make it a little bit more complicated both values are derived, using combineLatest and computed .

// RX way 
subj$ = new BehaviorSubject(0);
  constSubj$ = new BehaviorSubject(1);
  cSubj$ = combineLatest([this.subj$, this.constSubj$]).pipe(
    map(([a, b]) => a + b)
  );

  onClickSub(): void {
    this.i++;
    this.timerStart = performance.now();
    this.subj$.next(this.i);
  }

  changedSub(timeEnd) {
    if (this.timerStart) {
      this.subTimeAvg.push(timeEnd - this.timerStart);
    }
    const sum = this.subTimeAvg.reduce((acc, e) => acc + e, 0);
    console.log(sum / this.subTimeAvg.length);
  }

// Signal
  sig = signal(0);
  constSig = signal(1);
  cSig = computed(() => this.sig() + this.constSig());

  onClickSig(): void {
    this.i++;
    this.timerStart = performance.now();
    this.sig.set(this.i);
  }

  changedSig(timeEnd) {
    if (this.timerStart) {
      this.sigTimeAvg.push(timeEnd - this.timerStart);
    }
    const sum = this.sigTimeAvg.reduce((acc, e) => acc + e, 0);
    console.log(sum / this.sigTimeAvg.length);
  }
Enter fullscreen mode Exit fullscreen mode

As for the results, if you look under the hood, both solutions are actually rather simple, there is no meaningful difference between the two, very well within the measurement error.

Image description

Surprisingly, using input signal (and emitting in transform ) made in consistently faster than using set setter function with @Input to emit. But using input signal and an effect listening to that signal for the emit made the whole thing consistently over 1000 events twice as slow.

 in = input.required();

  constructor() {
    effect(() => {
      const i = this.in();
      const sigTimerEnd = performance.now();
      this.out.emit(sigTimerEnd);
    });
  }

  @Output()
  out = new EventEmitter<number>();

Enter fullscreen mode Exit fullscreen mode

Image description

No winners, no losers, but we all suspected these are all well-optimized solutions.

using RxJs over Signal has no performance implications ✅
using input Signal over @Input is ever so slightly more efficient ✅
using effect() strangely takes as long as setting a signal, updating a computation, setting a signal again, and emitting the value ❌

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay