The thing is that I want to know, how delete operator deletes a value from an array. Does it loop for a whole array? What is the best way to delete and item from an array paying attention to time complexity.
Thanks for your answers.
The delete operator just deletes all properties from an array element. Because looking up the element takes O(1) and deleting the properties takes O(1) the whole thing takes O(1). Be careful tho, delete does not change the length property of the array nor does it change the indexes of other elements within the array. So the behaviour is as follows:
const arr = [0,1,2,3,4,5]
delete arr[2]
console.log(arr[2]) // undefined
So delete is probably the best way in regards to time complexity, as methods utilizing splice or similar functions take O(n). Yet they are way safer.
To delete an element from an array, I have seen splice used most often with a delete_count passed as 1 (which means delete a single element).
let arr = [1, 2, 3];
arr.splice(0, 1); // delete `1` element starting at index `0`
// the above expression returns a new array containing the removed items
console.log(arr); // prints: [2, 3], since splice changes the array in place.
deletedoes.