tunnckoCore / ideas

:notebook: My centralized place for ideas, thoughts and todos. Raw, PoC implementations and so on... :star:
http://j.mp/1stW47C
6 stars 0 forks source link

ES Proposal: Basic Arithmethic Memoization #96

Closed tunnckoCore closed 6 years ago

tunnckoCore commented 6 years ago

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

const foo = (1 + 2) / 6

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

const foo = mem(() => ((1 + 2) / 6))

That memoized arrow function is called immediately and its evaluated result is assigned to the foo.

tunnckoCore commented 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).

tunnckoCore commented 6 years ago

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.

tunnckoCore commented 6 years ago

It's impossible.. :D But was good research and brainstorm.