Skip to content

⏱ Simple utility to cache moment.js results and speed up moment calls.

Notifications You must be signed in to change notification settings

yavorsky/moment-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

moment-cache

Moment-cache is a tiny tool to speed up your moment.js calls.

During the app lifecycle we can call moment oftentimes. Every call is time. Time is performance. This tool will increase performance of your app by caching moment.js instances.

Why?

  import moment from 'moment';
  import cache  from 'moment-cache';

  const dateString = '2016-08-24';
  const momentCalls = 99999;

  const check = (instance) => {
    let i = 0;
    const start = new Date;
    while (i <= momentCalls) {
      instance(dateString);
      i++;
    }
    return new Date - start;
  }

  console.log(check(moment));    // ~1588 ms
  console.log(check(cache));     // ~35 ms

Syntax:

Arguments:

  • date: See moment/parse.

  • format: See moment/format.

  • clone (by default - true): set false if you are not going to change instance in future. Will increase performance, but any object changes will affect cached result. See moment/clone.

  import cache from 'moment-cache'; // or moment().cache
  const myDate = '06-28-2016';
  const format = 'MM-DD-YYYY';
  const date = cache(myDate, format); // moment.js cached instance
  const anotherDate = cache(myDate, format); // rapidly retrieving previously processed result from the cache

Methods:

updateStorage: change cache destination.

Arguments:
  • storage: object where cache data is stored. By default - covert object behind the scenes.
  import cachable from 'moment-cache';
  const myStorage = {};
  cachable.updateStorage(myStorage);
  const date = cachable('2016-08-23');
  console.log(myStorage); // {1471899600000: Moment}

About

⏱ Simple utility to cache moment.js results and speed up moment calls.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published