For a string and an array, I have an "old" state, and a "new" state (after some modifications).
I need to often send to the server (with AJAX/XMLHttpRequest
) the changes, if possible in an efficient way (don't resend 200 KB of data if only one element in the array has changed/has been deleted/has been moved). Example:
var oldstate1 = 'hello how are you? very good and you? thanks for asking! this text will be removed.';
var newstate1 = 'hello how are you? very good and you? new text here. thanks for asking!';
var oldstate2 = [[1732, "item1"], [1732, "will be deleted"], [23, "will be moved"], [23, "hello"]];
var newstate2 = [[23, "will be moved"], [1732, "item1"], [23, "hello"], [126, "new item"]];
Of course, I could manually code a protocol between client and server with events such as delete
, insert
, move
, update
, etc. and send these events with AJAX, and the server would update its database accordingly. But this is quite tedious to do correctly.
Question: is there a more clever way to encode only changes between oldstate
and newstate
of a big string or array with Javascript? in a way that can easily be decoded on backend, running Python.
Something similar to a diff/patch
algorithm for strings or arrays, understood between JS (client-side) and Python (back-end).
Note:
a solution for strings might be enough, the "array case" would be covered by the "string case" with
JSON.stringigy(...)
related but doesn't solve directly the problem: How to get the difference between two arrays in JavaScript?