Comments (9)
and just to note that I'm using date-and-time in a NodeJS app; not in a browser
from date-and-time.
Do you have the parse function processed many times with the same string format? If so, as you say, it would be efficient if it could cache the regexes processing results and reuse them. I'll take a look.
from date-and-time.
Iām actually using your package in an AWS Lambda and by preparing an instance of date-and-time to parse my date strings; I can save a significant amount of time since, yes, the string format never changes.
from date-and-time.
Do you have any ETAs on when you think this could be fixed; and do you mind if I send you a PR for it?
from date-and-time.
We implemented it for the time being.
We are going to include it in the next version, but there are still many things to do, such as I/F design, compatibility, testing, documentation, so we can't say when it can be released. At least it is impossible in a few days. If this "alpha" version is acceptable, copy and run the following code to see the effect. With a simple benchmark, it seems to be about 60% faster. Please let me know if your implementation seems to be faster. :P
// Load "date-and-time" module.
const date = require('date-and-time');
// This is a compile function. The function name might be change.
date.compile = function (formatString) {
var re = /([A-Za-z])\1*|./g, keys, pattern = [formatString];
formatString = formatString.replace(/\[[^\[\]]*]|\[.*\][^\[]*\]/g, function (str) {
return str.replace(/./g, ' ').slice(2);
});
while ((keys = re.exec(formatString))) {
pattern[pattern.length] = keys[0];
}
return pattern;
};
// Rewrite the preparse function.
date.preparse = function (dateString, arg) {
var parser = locales[lang].parser, token, result, offset = 0,
pattern = typeof arg === 'string' ? date.compile(arg) : arg, formatString = pattern[0],
dt = { Y: 1970, M: 1, D: 1, H: 0, A: 0, h: 0, m: 0, s: 0, S: 0, _index: 0, _length: 0, _match: 0 };
dateString = parser.pre(dateString);
for (var i = 1, len = pattern.length; i < len; i++) {
token = pattern[i];
if (parser[token]) {
result = parser[token](dateString.slice(offset), formatString);
if (!result.length) {
break;
}
offset += result.length;
dt[token.charAt(0)] = result.value;
dt._match++;
} else if (token === dateString.charAt(offset) || token === ' ') {
offset++;
} else {
break;
}
}
dt.H = dt.H || parser.h12(dt.h, dt.A);
dt._index = offset;
dt._length = dateString.length;
return dt;
};
How To Use
const pattern = date.compile('MMM. D YYYY h:m:s A');
date.parse('Nov. 6 2019 1:11:11 p.m.', pattern);
date.parse('Nov. 6 2019 2:22:22 p.m.', pattern);
date.parse('Nov. 6 2019 3:33:33 p.m.', pattern);
from date-and-time.
Oops!
Sorry, the above code cannot be run. I'm going to push the whole code to the develop
branch later, please check out from there.
from date-and-time.
I tried your changes and they were about 15% faster for me on my laptop. It was almost insignificant in the Lambda.
from date-and-time.
Oh, no. That's too bad. š
Although we are not sure why it is almost insignificant in the Lambda, anyway, we are going to release it when it's ready.
from date-and-time.
We are not sure why the precompiling a string doesn't have an effect on AWS lambda. The reasons are because we don't know about AWS lambda well, and we don't know how you are using the module. If it is parsing many date time string per one call, for example, code must be like this:
// dummy lambda function
{
const pattern = date.compile('MMM. D YYYY h:m:s A');
const many_results = [];
for (const dateString of many_date_string) {
many_results.push(date.parse(dateString, pattern));
}
return many_results;
}
However, in your case, maybe it is parsing one date time string per one call? If so, the compile()
must be call every time. We guess this is the reason why it doesn't have an effect.
It may be wrong.
As a precompiling result is an array of string, serialize (like a JSON) and save it somewhere, and then, every time, deserialize it and call the parse()
:
// dummy lambda function
{
const pattern = date.compile('MMM. D YYYY h:m:s A');
save_somewhere(JSON.stringify(pattern)); // pseudo code
}
// dummy lambda function
{
const pattern = JSON.parse(load_from_somewhere(pattern)); // pseudo code
return date.parse(dateString, pattern);
}
from date-and-time.
Related Issues (20)
- Conflicts can occur when importing multiple plugins or changing locales
- formatTZ returns wrong locale HOT 3
- Swedish locale HOT 1
- Format(tz) fails when timezone string is greater than +1200 (i.e +1300) HOT 3
- Add transformTZ HOT 7
- Make it possible to use the library with react typescript HOT 2
- transformTZ doesn't work properly when original time zone is +0000 HOT 2
- Consider adding native support for Typescript types HOT 3
- ESM version exposed to environments that don't support ESM (e.g. jest 28) HOT 3
- Support clock change for .transform() HOT 2
- Can we substract year? HOT 1
- Invalid `Podfile` file: 767: unexpected token at 'Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in /ā¦/node_modules/date-and-time/package.json HOT 1
- Sudden invalid date HOT 4
- Format formats incorrect month HOT 1
- addMonth to 2022-03-01 does not add full month HOT 3
- addDays when crossing over from summer to standard time the UTC is incorrect HOT 2
- Module has no default export after upgrade to angular v15 HOT 3
- not an issue but need to highlight one missing HOT 8
- addMonts function calculates wrong date for some cases HOT 2
- It does not support Node.js 8 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from date-and-time.