Eshwar T. answered 16d
Industry-Proven Software Engineer and CS Tutor| 1800 rated Chess Coach
Hey!
The Core Challenge
Immutable.js structures are, well, immutable — you can't mutate them in place like Lodash's pullAllWith. But you can reassign a variable to a progressively smaller Immutable List on each iteration. The key is using let instead of const and leveraging .filterNot().
The Immutable.js Solution
Why This Works
.filterNot() is the Immutable.js equivalent of "remove these entries." It returns a new List with the matched events excluded, and by reassigning remainingEvents each loop, every subsequent day iterates over a smaller and smaller dataset — exactly the optimization you're after.
Even Better: Pre-Group with .groupBy()
If you really want to eliminate redundant passes altogether, pre-bucket your events once upfront:
This is O(n) total instead of O(days × events). You build the map once, then each day lookup is O(1). For 100,000 events across 30 days, this is the real win.
TL;DR
| Approach How | |
| Shrinking list (your pattern) |
let + .filterNot() reassignment |
| Best performance overall | Pre-group with .groupBy()
|
The groupBy approach is what I'd recommend in production — it respects day order (since you still iterate calendarRange in order) and avoids re-scanning the list entirely.
Let me know if you have any other questions.