Lodash _.pull Array Mutation on Uncaught Exception

When the Lodash _.pull method is interrupted by an uncaught exception, the target array is left in a partially mutated state containing only the deletions completed before the error occurred. Because Lodash executes in-place removals eagerly using standard JavaScript array operations without an atomic rollback mechanism, any index shifts and removals processed prior to the exception persist on the original reference.

Internal Mechanism of _.pull

The _.pull method in Lodash is a mutating operation that strips specified values from an array. Internally, it relies on the basePullAll implementation, which iterates through the supplied target values and searches for matches within the original array using the SameValueZero comparison.

When a match is identified, Lodash immediately calls Array.prototype.splice directly on the array to remove the item at the matching index. This triggers an immediate shift of all subsequent elements to fill the vacant index and decrements the array's length property.

How Uncaught Exceptions Cause Partial Mutation

Because JavaScript execution is single-threaded and synchronous, an uncaught exception halts execution at the exact step where the error is thrown. Exceptions during a _.pull call can occur due to:

When an exception is thrown during any of these operations, the execution stack unwinds immediately. Lodash does not maintain a shadow copy or transaction log of the original array, meaning no rollback routine exists to restore previously deleted elements.

Observable State of the Array After an Interruption

If an uncaught exception interrupts _.pull, inspecting the original array reveals specific structural changes:

  1. Permanent Prior Deletions: Any elements matched and spliced before the iteration reached the error-producing step are permanently gone from the array.
  2. Shifted Elements: Any elements that followed the already-deleted items remain at their shifted, lower index positions.
  3. Truncated Length: The length property of the array reflects only the removals completed prior to the failure.
  4. Untouched Trailing Elements: Elements that were scheduled to be evaluated or removed after the failure point remain in the array at their current shifted positions.

Preventing Corrupted States

To avoid corrupted, partially mutated arrays in systems where exceptions might occur during iteration or property access, use non-mutating alternatives such as Lodash's _.without or native JavaScript Array.prototype.filter. Non-mutating methods construct and return a new array instance upon successful completion, ensuring the original array remains untouched if an error terminates the process prematurely.