dillon1000/react
Commit
Browse files Example of "unnecessary" memoization of lambdas
I noticed this while demoing Forget to React Org alum Christoph Nakazawa — in array.map calls (and other APIs that take a lambda as input) we sometimes end up memoizing the lambda. It's technically correct since the function _could_ return the lambda, and then we'd need it to be memoized. It's tricky because array.map is often called on nested objects, where even if we had type inference on the outer value we wouldn't know for sure that the inner property is an Array and not some other data type with a custom .map. For example in `data.feedback.comments.edges.map(edge => ...)`, even if we knew that `data` was an Object, we wouldn't know that data.feedback.comments.edges is an Array without cross-file type knowledge. But it's definitely wasteful to memoize these lambdas, so we should brainstorm options. One option that stands out right away: if the lambda has zero dependencies, then we could lift it out to module scope and refer to it by name.
Changed paths2 files
First-parent comparisoncompiler/forget/src/__tests__/fixtures/compiler/todo.unnecessary-lambda-memoization.expect.md AddedA compiler/forget/src/__tests__/fixtures/compiler/todo.unnecessary-lambda-memoization.js AddedPatch
Files changed
Rendering syntax-highlighted changes…