Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memoize eagerly evaluates more than necessary #191

Open
fenduru opened this issue Oct 10, 2016 · 3 comments
Open

memoize eagerly evaluates more than necessary #191

fenduru opened this issue Oct 10, 2016 · 3 comments
Assignees

Comments

@fenduru
Copy link

fenduru commented Oct 10, 2016

When memoizing a sequence, accessing any value in that sequence seems to cause the entire sequence to be evaluate eagerly. I think that the correct behavior would be to evaluate only what is needed on demand, and cache that value for future accesses.

My use case is that I have a very large set of data that I display as a grid. I want to lazily perform operations on the data, and then use either slice or drop/take to compute only the data that is currently in view. Then when the user scrolls, I want to slice into the memoized sequence again, avoiding recomputation of items that have already been computed.

Before memoization, it seems like some of the optimizations I'm looking for are implemented (i.e. .get(1) on a mapped array doesn't call the map function for index 0).

function double(x) {
  console.log('doubling', x);
  return x * 2;
}

var seq = Lazy([1, 2]).map(double);

seq.get(1)
// 'doubling 2'

seq.memoize().get(1)
// 'doubling 1'
// 'doubling 2'
@dtao
Copy link
Owner

dtao commented Jan 25, 2017

I think I buy that. I'll take a look into how hard this would be.

@dtao dtao self-assigned this Jan 25, 2017
@dtao dtao closed this as completed in b1640b7 Feb 2, 2017
@fenduru
Copy link
Author

fenduru commented Feb 7, 2017

@dtao This is definitely an improvement, however if I'm reading that commit correctly, it won't actually solve the test case in my original issue. seq.memoize().get(1) will still output doubling 1 unnecessarily

@dtao
Copy link
Owner

dtao commented Feb 7, 2017

@fenduru You're right. I do not currently have a special case for array-like memoized sequences. memoize() now only iterates as far as necessary to get to the desired value.

I get now what you're asking for; it will just require some more time.

@dtao dtao reopened this Feb 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants