Memoizing operations for performance
Massive speedup of idf calculation