Firebase arrayRemove takes very long time in modular version
I'm refactoring my firebase code to support the modular firebase version.
After I refactored the "arrayRemove" function, I noticed that it started to take a very long time to execute. Something about 1 minute. At v8, that was about 1 seconds
Here is my code:
const userRef = doc(db, 'users', userId)
try {
await updateDoc(userRef, {
items: arrayRemove(itemId),
})
console.log('REMOVED')
} catch (error) {
console.log(error)
}
Execution of this code takes around one minute. The same action with the same data in the same environment for the older version takes about one second. The rest functions works fast.
Firebase version: 9.6.1
Any ideas why it takes so long and how to make it faster?
Solution 1:
After reading article "Best Practices: Arrays in Firebase"
I found that practice:
to remove keys, we save the entire array instead of using .remove()
So... I removed item in my code, then updated array and it took less than a second!
// removing item I need to remove at my local array
const updArr = arr(
(item) => item !== itemId
)
const userRef = doc(db, 'collection', 'id')
// updating array
await updateDoc(userRef, {
gifts: updArr,
})
// p.s. Obviously, there is some bug at arrayRemove function, but actually we don't have to use that function 🤫