Working on app where speed is crucial, the arrays are huge and the objects contained within the arrays.
I experimented with grep
and filter
and can not see significant speed difference, varies +- 5ms , also tried looping through array and using .splice(i,1);
(same results).
I have a fast machine, if it always take more or less the same time on fast machine, does that mean it will take more or less same time on older machine?
Is there faster way to remove an object from array?
Want to do something like this:
var filterTime = performance.now();
doStuff1();
var filterTimeEnd = performance.now();
var grepTime = performance.now();
doStuff2();
var grepTimeEnd = performance.now();
and then store the differences in cookie, so the next time the page loads or is refreshed, execute most efficient way: removing object from array.
UPDATE
snippet of filter experiment
companyMasters = companyMasters.filter(function (obj) {
return obj.masterId != CompanyObj.masterId;
});
pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.
Approach 1: Store the index of array elements into another array which need to be removed. Start a loop and run it to the number of elements in the array. Use splice() method to remove the element at a particular index.
To remove an object from an array by its value:Call the findIndex() method to get the index of the object in the array. Use the splice() method to remove the element at that index. The splice method changes the contents of the array by removing or replacing existing elements.
The existing answer already provide good solutions to reducing the runtime complexity of the underlying problem.
However I want to briefly answer the original question as well, since this is the first page when googling for how to remove from an array the most performant way.
Without maintaining order the fastest way to remove by index is removing by assigning the last element to the to be removed index and popping from the array, since this has O(1) runtime complexity.
Array.prototype.mySwapDelete = function arrayMySwapDelete (index) {
this[index] = this[this.length - 1];
this.pop();
}
With maintaining order the fastest way to remove by index is shifting in place:
Array.prototype.myShiftDelete = function arrayMyShiftDelete (index) {
var stop = this.length - 1;
while (index < stop) {
this[index] = this[++index];
}
this.pop();
}
I have created a JS perf snippet to benchmark the different functions: https://jsperf.com/array-remove-by-index
When wanting to filter, it is also significantly faster to filter in place and shift than calling the native .filter()
function, which allocates a new array. This in place filter also maintains order:
Array.prototype.myShiftFilter = function arrayMyShiftFilter (predicate) {
let i, j;
for (i = 0, j = 0; i < this.length; ++i) {
if (predicate(this[i])) {
this[j] = this[i];
++j;
}
}
while (j < this.length) {
this.pop();
}
}
See also JS Perf snippet for benchmark: https://jsperf.com/array-filter-in-place
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With