Since I first got introduced to libraries/frameworks like Angular (v2) or Svelte, I have always wondered how they manage lists and list keys in iterative blocks (*ngFor="..."
and {#each ...} ... {/each}
respectively). How do they know which items to create, update, and delete? What kind of particular algorithm(s) is/are being used for list rendering? I've tried looking at Svelte compiled output in the each block example, but it is a bit hard to understand.
Note 1: Unless virtual DOM libraries use this kind of technique for rendering lists, I would prefer non-(p)react answers, please.
Note 2: I would prefer a general algorithm because I'm building a UI library from scratch (i.e. no dependencies) that interfaces directly with the DOM.
Update 1
To clarify, let's say that I have the following data on the first render:
[
{ key: 1, item: 'a', },
{ key: 2, item: 'b', },
{ key: 3, item: 'c', },
]
An update later, I get the following data:
[
{ key: 1, item: 'a', },
{ key: 3, item: 'd', },
{ key: 4, item: 'f', },
]
How can I tell some code that { key: 2, item: 'b', }
was removed, { key: 3, item: 'c', }
was updated to { key: 3, item: 'd', }
, and { key: 4, item: 'f', }
was added?