Yomguithereal / baobab

JavaScript & TypeScript persistent and optionally immutable data tree with cursors.
MIT License
3.15k stars 117 forks source link

Question regarding releasing cursors #504

Closed soulprovidr closed 6 years ago

soulprovidr commented 6 years ago

Hi, I have a question about cursors and potential memory leaks arising from their use.

In our application we have a lot of modules that look like this:

export default function setDeeplyNestedProperty(tree, value) {
    const shortcutCursor = tree.select(['foo', 'bar', 'baz']);
    const newValue = doSomeCalculationOrTransformation(value);
    shortcutCursor.set(newValue);
}

My question is, will using .select() in this fashion cause a memory leak? Should we call shortcutCursor.release() at the end of the function? (That's what the docs seem to suggest).

Thanks!

Yomguithereal commented 6 years ago

Normally, if I remember correctly how I implemented the library, if you don't set listeners on said cursors, you won't leak memory. What's more, cursors are memoized, meaning that if you select multiple times the same cursor, the same object will be returned. But if you want to be sure, using release should be safe.

soulprovidr commented 6 years ago

Thanks, good to know!