Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS expand won't complete after a certain amount of calls. Does it have a limit?

I'm trying to count down from a number, lets say 8000, then I reduce all the values into an array. If I count down by 100, 50 or even 10, expand completes and I can see the array in the console, but if I count down by 1 (step = 1), it just stops at a certain number and does nothing, won't continue, won't complete. There are no errors in the console.

Anyone knows what's going on? Does expand have a limit of recursive calls?

const fromValue = 8000;
const step = 1;   // Change this value to 1, 10, 100...

rxjs.of(fromValue).pipe(
  rxjs.expand(value => {
    console.log(value);
    return value > 0 ? rxjs.of(value - step) : rxjs.EMPTY;
  }),
  rxjs.reduce((acc, value) => [...acc, value], [])
).subscribe(result => console.log(result));
<script  src="https://unpkg.com/[email protected]/dist/bundles/rxjs.umd.min.js"></script>
like image 561
Danny Mencos Avatar asked Oct 18 '25 15:10

Danny Mencos


1 Answers

JavaScript is running out of Heap size memory, maybe find a more efficient code to perform this logic?

enter image description here

Note: dont use stack overflow console, use the console of the browser!

const fromValue = 8000;
const step = 1; // Change this value to 1, 10, 100...
console.clear();
rxjs.of(fromValue).pipe(
  rxjs.expand(value => {
    console.log(value, window.performance.memory);
    return value > 0 ? rxjs.of(value - step) : rxjs.EMPTY;
  }),
  rxjs.tap(() => console.log('before array', window.performance.memory)),
  rxjs.toArray(),
  rxjs.tap(() => console.log('after array', window.performance.memory)),
).subscribe(result => {
  console.log('final array', window.performance.memory);
  console.log(result);
});
<script src="https://unpkg.com/[email protected]/dist/bundles/rxjs.umd.min.js"></script>

Alternate way

let fromValue = 8000;
const step = 1; // Change this value to 1, 10, 100...
const output = [];
console.clear();
console.log('start')
rxjs.interval().pipe(
  rxjs.tap(() => {
console.log('iteration', fromValue)
    output.push(fromValue);
    fromValue = fromValue - step;
  }),
  rxjs.takeWhile((x) => fromValue >= 0),
  rxjs.last(),
).subscribe(() => {
  console.log(output);
});
<script src="https://unpkg.com/[email protected]/dist/bundles/rxjs.umd.min.js"></script>
like image 149
Naren Murali Avatar answered Oct 21 '25 03:10

Naren Murali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!