menu

manual-pop-if

Prefer Array.popIf over conditionally popping the last element

Testing an array's last element before popping it performs two separate accesses to the same element. Instead, you SHOULD use popIf to test and remove the last element together.

Reported

1function popExpected(values: int32[], expected: int32): int32 | undefined {2    return values.last() === expected ? values.pop() : undefined;3}

Accepted

1function popExpected(values: int32[], expected: int32): int32 | undefined {2    return values.popIf((value) => value === expected);3}