Memoize

Use in case you find yourself in a phone interview with an online bookstore.

by ronilan

JavaScript

// memoize

function long() {
  console.log("long");
  return "my data";
}

function memoize(fn) {

  var cached = null;

  result = function() {
    if (!cached) {
      cached = fn();
    }
    return cached;
  }

  return result;
}

var action = memoize(long);

console.log(action());
console.log(action());
console.log(action());