tracked-tools / tracked-built-ins

Tracked versions of JavaScript's built-in classes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add static constructors for `TrackedArray` and `TrackedObject` that don't copy

boris-petrov opened this issue · comments

I know that in the README it's explicitly written that they copy their arguments for safety (which is great and makes sense) but sometimes one might want to save on memory and performance when they know that the original won't be accessed any more from anywhere else. I'm fine with calling the methods UNSAFE_CREATE or something but just the ability to do that would be nice.

I actually have copy-pasted locally the implementations from this library and added that single static method.

I could open a PR if that's acceptable.

This is an interesting idea. I can see the argument for creation time for sure since that ends up being O(N) on the size of the consumed iterable. In terms of memory usage, the issue seems less dire: if you stop using the original anywhere else, it will be garbage collected. The only time I can see that being an issue is if you're dealing with an extremely large array such that having two copies in memory for any amount of time causes problems. 🤔

When I copied TrackedArray to begin the spike for EmberData I also removed the copy. It is never the case for us that the original array can be mutated, and it may be hundreds of thousands of records large (I'd say millions but the only app I've seen with a million records of one type needed serious help due to the memory pressure).

You can find that implementation here: https://github.com/emberjs/data/pull/8134/files#diff-e677c61aa8be87c5dc671c05409d1bcb26134ebef595bd6fc5ed0df0d4049889

It also contains a number of other changes as we (1) needed actual proxying behavior not just tracking and (2) it never makes sense to have a tag per-index so we removed that.