Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yavorsky/moment-cache
⏱ Simple utility to cache moment.js results and speed up moment calls.
https://github.com/yavorsky/moment-cache
cache chicks date datetime moment moment-js performance speed time
Last synced: about 11 hours ago
JSON representation
⏱ Simple utility to cache moment.js results and speed up moment calls.
- Host: GitHub
- URL: https://github.com/yavorsky/moment-cache
- Owner: yavorsky
- Created: 2016-02-23T12:48:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-11-27T10:36:14.000Z (almost 8 years ago)
- Last Synced: 2024-10-11T22:13:11.206Z (27 days ago)
- Topics: cache, chicks, date, datetime, moment, moment-js, performance, speed, time
- Language: JavaScript
- Homepage:
- Size: 35.2 KB
- Stars: 28
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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?
```javascript
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](http://momentjs.com/docs/#/parsing/).* format: See [moment/format](http://momentjs.com/docs/#/parsing/string-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](http://momentjs.com/docs/#/parsing/moment-clone/).
```javascript
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.
```javascript
import cachable from 'moment-cache';
const myStorage = {};
cachable.updateStorage(myStorage);
const date = cachable('2016-08-23');
console.log(myStorage); // {1471899600000: Moment}
```