medikoo / memoizee

Complete memoize/cache solution for JavaScript
ISC License
1.73k stars 61 forks source link

Add option for custom profiler #96

Closed faboulaws closed 6 years ago

faboulaws commented 6 years ago

First off, thank for writing such a great module. I love it.

I know the profiler is only for development purpose but it can do so much more. I would like to suggest the option to pass a custom profiler or listener. The current profiler ('memoizee/profiler') not only works as a global profiler but does not work when it is required after a function was memoized . In other words, any function memoized before require('memoizee/profiler') is not profiled(correct me if I am wrong). This has some limitations.

What I am suggesting is an additional option so that we can have a different listener per function memoized. The listener (or profiler) can have a event handler for each of the configure-map (see ) events:

const listener = {
    onGet: () => {
        console.log('yay cache hit')
    }, 
    onSet: () => {
         console.log('Oops, no caching used')
    }
}
const memFn = memoizee(function(){
// ...
}, {listener})

The current profiler can be used as a default profiler:

Example

Just a suggestion.

Thanks

medikoo commented 6 years ago

@faboulaws can you explain well your use case, what exactly you want to achieve?

Here you propose a solution, but I'd love to know what problem we're really solving, that we way we can both pickup best solution :)

Also mind there's a v1 version design proposed, and in that version you'll be able to observe events for all cache operations per memoized function.

Still as delivery date of v1 is uncertain, I'll be happy to add some improvements to v0.4, assuming it doesn't require any significant work and doesn't break things.

medikoo commented 6 years ago

@faboulaws as I didn't get any feedback I'm closing this. I also believe that all you need will be provided with v1