Description
Consider
const arr = [1,2,3,4,5,6];
When you arr.shift()
what technically happens is
MOV 2 -> 1
MOV 3 -> 2
MOV 4 -> 3
MOV 5 -> 4
DELETE [6]
Same happens if you splice
. But starting from the index you pass. Now imagine you have an array of 1000 items. Shifting it would produce 1000 operations! All sent via web socket. Assuming it's an array of integers (not deep objects), smallest operation is 50 bytes, 50 * 1000 = ~50KB of transfer for deleting a single list item! Can you imagine how slow is this? It takes ~100ms to do a single shift operation on the same host, imagine via network. And this applies to jsonpatch
and to proxies. It's not proxies fault.
Proposed solution:
Simply produce a single remove
operation.
How?
With jsonpatch
this cannot happen. But with proxies when you call shift
and splice
you're invoking the proxy getter
with key
= shift
, and I can return to you any method I want. And I can return a special shift
/splice
function that does exactly the same job for you but emits a single patch instead of N.
Challenges
- JSON-Patch RFC compliance: Luckily, this complies with the RFC, the RFC actually defines a single remove op as the correct behaviour, and it's the applying end duty to shift the array, not the observer to issue all those operations.
jsonpatch.applyPatch
support: I tested. Applying a remove operation on the array yields the correct results.- Server support: I don't know about this and will need some testing.
Objective:
This will give a great boost to general Starcounter performance.