Code Monkey home page Code Monkey logo

Comments (9)

ronak2121 avatar ronak2121 commented on June 11, 2024

and just to note that I'm using date-and-time in a NodeJS app; not in a browser

from date-and-time.

knowledgecode avatar knowledgecode commented on June 11, 2024

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.

ronak2121 avatar ronak2121 commented on June 11, 2024

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.

ronak2121 avatar ronak2121 commented on June 11, 2024

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.

knowledgecode avatar knowledgecode commented on June 11, 2024

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.

knowledgecode avatar knowledgecode commented on June 11, 2024

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.

ronak2121 avatar ronak2121 commented on June 11, 2024

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.

knowledgecode avatar knowledgecode commented on June 11, 2024

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.

knowledgecode avatar knowledgecode commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.