Skip to content

Commit

Permalink
feat(utils): adds memoize
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 24, 2019
1 parent aad9f6e commit 24212ed
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/utils/memoize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import hash from 'object-hash';
import { IOfType } from '~/types';

// TODO modularize
export default function memoize<T extends Function>(fn: T): T {
const cache: IOfType<any> = {};

return function(...args: any[]) {
const key = hash(args);
if (cache.hasOwnProperty(key)) return cache[key];

return (cache[key] = fn(...args));
} as any;
}

0 comments on commit 24212ed

Please sign in to comment.