fix(takeLast): prevent RangeError when count is Infinity - #7608
Conversation
`takeLast(Infinity)` threw `RangeError: Invalid array length` because `new Array(Infinity)` is not a valid JavaScript expression. Fix by conditionally allocating the ring buffer only when count is finite. For non-finite counts the operator falls back to a growable array. The ring-buffer index arithmetic already handles this correctly since `n % Infinity === n` for all finite n, so no further changes are needed. Fixes: https://github.com/ReactiveX/rxjs/issues/XXXX
|
IMO On the other hand that would change the existing behavior of only emitting the items on complete to emitting immediately... |
|
| Command | Status | Duration | Result |
|---|---|---|---|
nx compile rxjs |
❌ Failed | 8s | View ↗ |
💡 Dealing with memory or CPU issues? See memory and CPU details with the resource usage add-on ↗.
☁️ Nx Cloud last updated this comment at 2026-07-22 19:59:11 UTC

Problem
takeLast(Infinity)throws aRangeError: Invalid array lengthat subscription time:The cause is line 50 of
takeLast.ts:JavaScript forbids creating an array with a non-integer or infinite length argument.
Fix
Use a growable array (
[]) whencountis non-finite. The ring-buffer index arithmetic already handlesInfinitycorrectly becausen % Infinity === nfor every finiten, so no further changes to the logic are required:After the fix,
takeLast(Infinity)behaves liketakeLast(n)wheren >= source.length: it buffers every value and emits them all on completion.Tests
Two new tests added to
takeLast-spec.ts: