Skip to content

deliveryhero/moment-cache

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 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 momentCalls = 99999;
const invalidDateStringWithOffset = "2013-02-08 09+07:00";
const dateStringWithOffset = "2020-05-12T23:50:21.817Z";
const timestamp = 628021800000;
const timeStampDate = "1989-11-25T18:30:00.000Z";

const dateString = "18-03-2024";
const format = "DD-MM-YYYY";

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

const cacheTimestamp = check(cache, timestamp); // ~42 ms
const momentTimestamp = check(moment, timestamp); // ~49 ms

const cacheDateString = check(cache, dateString, format); // ~65 ms
const momentDateString = check(moment, dateString, format); // ~390 ms

const cacheDateStringWithOffset = check(cache, dateStringWithOffset); // ~218 ms
const momentDateStringWithOffset = check(moment, dateStringWithOffset); // ~777 ms

const cacheInvalidDateStringWithOffset = check(
  cache,
  invalidDateStringWithOffset
); // ~75 ms
const momentInvalidDateStringWithOffset = check(
  moment,
  invalidDateStringWithOffset
); // ~571 ms

const cacheTimestampDate = check(cache, timeStampDate); // ~202 ms
const momentTimestampDate = check(moment, timeStampDate); // ~755 ms

Syntax:

Arguments:

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("23-08-2016", "DD-MM-YYYY");
console.log(myStorage); // {23-08-2016 DD-MM-YYYY: MomentObject}

About

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

Resources

Stars

Watchers

Forks

Languages

  • JavaScript 100.0%