Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                // Function that sums two numbers
const summation = function (a, b) {
 if (typeof(a) === 'number' && typeof(b) === 'number') {
   console.log('From Summation function');
   return a + b;
 }
  return "Invalid Entry";
}

// To create a Property name from the arguments passed to the function
const constructPropertyFromArgs = function (fnToMemoize, args) {
  let propToCheck = [];
  propToCheck = propToCheck.concat(fnToMemoize.name, args);
  return propToCheck.join('|'); // A delimiter to join args
}


// To manage space complexity
// To maintain only specified number of elements in the cache and deleting the others using First In First Out approach
const manageInsertion = function(memoizedCache, propToCheck, cacheSize = 10) {
  if (memoizedCache.insertionOrder.length >= cacheSize) {
    const oldestElementName = memoizedCache.insertionOrder.shift();
    delete memoizedCache[oldestElementName];
   }
   memoizedCache.insertionOrder.push(propToCheck);
}

//  `memoize` function  decides if it has to return cached value or call the summation function
const memoize = function (fnToMemoize, cacheSize) {
  if (!(typeof fnToMemoize === 'function')) {
    throw new Error('Argument passed to memoize function should be a function');
  }
  const memoizedCache = {         // A closure object
    insertionOrder: []            // To preserve the order of Insertion, so that FIFO can be implemented
  }; 
  
  return function(...args) {
    const propToCheck = constructPropertyFromArgs(fnToMemoize, args);
    if (!memoizedCache[propToCheck]) {
      memoizedCache[propToCheck] = fnToMemoize(...args);
      manageInsertion(memoizedCache, propToCheck, cacheSize);
    } else  {
      console.log('From Cache ');
      // LRU logic implementation, moving the current element name to end of the array
      const currIndex = memoizedCache.insertionOrder.indexOf(propToCheck);
      memoizedCache.insertionOrder.splice(currIndex, 1);
      memoizedCache.insertionOrder.push(propToCheck);
      
    }
    return memoizedCache[propToCheck];
  }
}

const memSummation = memoize(summation, 2);  // `memoize` is a HOC

console.log(memSummation(10, 50));
console.log(memSummation(10, 53));
console.log(memSummation(10, 56));
console.log(memSummation(10, 56));
console.log(memSummation(11, 80));


              
            
!
999px

Console