Skip to content

Instantly share code, notes, and snippets.

@baoqger
Created May 29, 2020 02:44
Show Gist options
  • Save baoqger/3e3d793719d26f4762c753e6e5d3fc73 to your computer and use it in GitHub Desktop.
Save baoqger/3e3d793719d26f4762c753e6e5d3fc73 to your computer and use it in GitHub Desktop.
import { defaultMemoize } from "./memoizedSelector";
import { slowFunction } from "./slowFunction";
// run slowFunction without memoization
console.log("First call of slowFunction(2)");
let pre = new Date();
slowFunction(2);
console.log("It takes" + ((new Date()).valueOf() - pre.valueOf())/1000 + "seconds \n");
console.log("Second call of slowFunction(2)");
pre = new Date();
slowFunction(2);
console.log("It takes" + ((new Date()).valueOf() - pre.valueOf())/1000 + "seconds \n");
// run slowFunction with memoization
const fastFunction = defaultMemoize(slowFunction);
console.log("First call of fastFunction(2)");
pre = new Date();
fastFunction.memoized(2);
console.log("It takes" + ((new Date()).valueOf() - pre.valueOf())/1000 + "seconds \n");
console.log("Second call of fastFunction(2)");
pre = new Date();
fastFunction.memoized(2);
console.log("It takes" + ((new Date()).valueOf() - pre.valueOf())/1000 + "seconds \n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment