-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove array items without consuming extra memory (#59)
* feat: add remove-array-items that can remove items from an array without generating memory garbage * chore: replace arr.splice(start, deleteCount) with removeArrayItems(arr, start, deleteCount)
- Loading branch information
Showing
3 changed files
with
87 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Remove a range of items from an array. | ||
* | ||
* @function removeItems | ||
* @param {Array<*>} arr The target array. | ||
* @param {number} startIndex The index to begin removing from (inclusive). | ||
* @param {number} removeCount How many items to remove. | ||
*/ | ||
const removeArrayItems = (arr, startIndex, removeCount) => { | ||
const length = arr.length; | ||
|
||
if (startIndex >= length || removeCount <= 0 || startIndex < 0) { | ||
return; | ||
} | ||
|
||
removeCount = (startIndex + removeCount > length ? length - startIndex : removeCount); | ||
|
||
const len = length - removeCount; | ||
|
||
for (let i = startIndex; i < len; ++i) { | ||
arr[i] = arr[i + removeCount]; | ||
} | ||
|
||
arr.length = len; | ||
}; | ||
|
||
export default removeArrayItems; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { test } from 'tap'; | ||
import removeArrayItems from '../src/remove-array-items'; | ||
|
||
test('should return if the start index is greater than or equal to the length of the array', (t) => { | ||
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
removeArrayItems(arr, arr.length + 1, 5); | ||
t.equals(arr.length, 10); | ||
t.end(); | ||
}) | ||
|
||
test('should return if the remove count is 0', (t) => { | ||
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
removeArrayItems(arr, 2, 0); | ||
t.equals(arr.length, 10); | ||
t.end(); | ||
}); | ||
|
||
test('should remove the number of elements specified from the array, starting from the start index', (t) => { | ||
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
removeArrayItems(arr, 3, 4); | ||
t.deepEquals(arr, [1, 2, 3, 8, 9, 10]); | ||
t.end(); | ||
}); | ||
|
||
test('should remove other elements if delete count is larger than the number of elements after start index', (t) => { | ||
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
removeArrayItems(arr, 7, 10); | ||
t.deepEquals(arr, [1, 2, 3, 4, 5, 6, 7]); | ||
t.end(); | ||
}); | ||
|
||
test('should remove no element if count is less than or equal to zero', function (t) { | ||
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
removeArrayItems(arr, 7, -2); | ||
t.deepEquals(arr, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); | ||
t.end(); | ||
}); | ||
|
||
test('should remove no element if start is less than or equal to zero', (t) => { | ||
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
removeArrayItems(arr, -7, 5); | ||
t.deepEquals(arr, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); | ||
t.end(); | ||
}); | ||
|
||
test("should remove the remaining elements start with 'start' if count is greater than arr.length", (t) => { | ||
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
removeArrayItems(arr, 4, 100); | ||
t.deepEquals(arr, [1, 2, 3, 4]); | ||
t.end(); | ||
}); |