Closed tunnckoCore closed 6 years ago
A computation will be done only once.
This transformation gives us one more benefit - if same expression occur somewhere else it won't be evaluated/computed. It is because the result of that computation will be added to the cache, based on its representation (the actual body of the function that wraps that expression).
The cached record for that expression is looking like that
const cache = {
'(1 + 2) / 6': 0.5
}
if something in that expression is changed, then another record will be added, as you should expect.
It's impossible.. :D But was good research and brainstorm.
It can be useful in big arithmetic operations. Basically it will be evaluated once until something in that expression is changed, cache time end or max hit count exceed.
Take this basic example
It can be transformed first to a function that returns it, then that function is passed to another function called for example
mem
. It's important to make difference between that function and another that memoize another functions. Its semantic is different.So the above snippet could be transformed to this
That memoized arrow function is called immediately and its evaluated result is assigned to the
foo
.