Questions tagged [memoization]

In computing, memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously-processed inputs.

In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

This tag should be used for programs that are using memoization.

Source

Wikipedia

1491 questions
26
votes
7 answers

Thread-safe memoization

Let's take Wes Dyer's approach to function memoization as the starting point: public static Func Memoize(this Func f) { var map = new Dictionary(); return a => { R value; if (map.TryGetValue(a, out…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
25
votes
5 answers

Is JSON.stringify() deterministic in V8?

I've not seen (yet?) JSON.stringify to be non-deterministic in Node.JS. There is no guarantee it to be deterministic on the specification level. But what about V8; Is its implementation there deterministic? Is there a guarantee for it to remain…
brillout
  • 7,804
  • 11
  • 72
  • 84
24
votes
3 answers

When to use memoization in Ruby on Rails

In mid July 2008 Memoization was added to Rails core. A demonstration of the usage is here. I have not been able to find any good examples on when methods should be memoized, and the performance implications of each. This blog post, for example,…
Gdeglin
  • 12,432
  • 5
  • 49
  • 65
24
votes
2 answers

React.useMemo in class component

Is there a way to use this hook or some its analogue of the React API in the case of a class component? I'm wondering, should I use some third party memo helpers in the case of a class component (eg. lodash memo, memoizeOne, etc.) or there is exist…
Velidan
  • 5,526
  • 10
  • 48
  • 86
24
votes
3 answers

What are the different techniques for memoization in Java?

I know of this one http://onjava.com/pub/a/onjava/2003/08/20/memoization.html but is there anything else?
ranv01
  • 259
  • 1
  • 2
  • 5
23
votes
4 answers

Can I memoize a Python generator?

I have a function called runquery that makes calls to a database and then yields the rows, one by one. I wrote a memoize decorator (or more accurately, I just stole one from this stackoverflow question) but on subsequent calls it just yields an…
bryn
  • 1,246
  • 1
  • 13
  • 21
22
votes
2 answers

Java memoization method

I came across an interesting problem and was wondering if and how could this be done in Java: Create a method which can memoize any function/method . The method has the following arguments : the method/function and the argument(s) for it. For…
Alex
  • 550
  • 1
  • 7
  • 19
22
votes
7 answers

Haskell caching results of a function

I have a function that takes a parameter and produces a result. Unfortunately, it takes quite long for the function to produce the result. The function is being called quite often with the same input, that's why it would be convenient if I could…
ondra
  • 9,122
  • 1
  • 25
  • 34
21
votes
1 answer

Baffled by the Arrow in Do Statements in Haskell

I am working on understanding the State monad and have written two simple versions of the famous fibonacci to memoize the function. The one with let in the body runs very slowly. The one with <- runs very fast. My question: Why? Is let causing full…
dsagman
  • 481
  • 1
  • 8
21
votes
5 answers

Function to cache its argument's return value

I want to write a function once that accepts a callback as input and returns a function. When the returned function is called the first time, it should call the callback and return that output. If it is called any additional times, instead of…
Amit Kumar
  • 301
  • 2
  • 13
21
votes
5 answers

potential O(n) solution to Longest Increasing Subsequence

I was trying to answer this problem, using just recursion (Dynamic programming) http://en.wikipedia.org/wiki/Longest_increasing_subsequence From the article, and around SO, I realise the most efficient existing solution is O(nlgn). My solution is…
21
votes
5 answers

How do you make a generic memoize function in Haskell?

I've seen the other post about this, but is there a clean way of doing this in Haskell? As a 2nd part, can it also be done without making the function monadic?
Jonathan Tran
  • 15,214
  • 9
  • 60
  • 67
20
votes
2 answers

Efficient memoization in Python

I have some task to solve and the most important part at the moment is to make the script as time-efficient as possible. One of the elements I am trying to optimize is memoization within one of the functions. So my question is: Which of the…
Tadeck
  • 132,510
  • 28
  • 152
  • 198
20
votes
2 answers

How to memoize custom React hook

const useSomeHook = ({number}) => { const [newNumber, setNewNumber] = useState(0) useEffect(() => { setNewNumber(number + 1) }, [number]) } const SomeComponent = ({number, value, ...restProps}) => { useSomeHook({number}) return…
Andrey Radkevich
  • 3,012
  • 5
  • 25
  • 57
20
votes
2 answers

Is it safe to useMemo for JSX?

I'm trying to do a memoize Modal and I have a problem here. When I change input I dont need to re-render the Modal component. For example: Modal.tsx looks like this: import React from "react"; import { StyledModalContent, StyledModalWrapper,…
Tazo Leladze
  • 251
  • 1
  • 3
  • 6