There is a technique that allows optimization of repeated calculation results for a set of inputs called memoization . This technique is particularly helpful when working with numeric computations, pathing, such as for tree searches, etc. where the time