Wiki

Clone wiki

scheduler-cron / Comparison of Scheduler Cron and JS realization

Comparison of Scheduler Cron and JS realization

TimeExecutor

The example of usage:

new TimeExecutor(new Date(2016, 11, 4), function() {
    // Operations
});

Pure JS:

setTimeout(function() {
    // Operations
}, (new Date(2016, 11, 4)).getTime() - Date.now());

TimeExecutore looks simpler and it is easier to read it.

IntervalJob

The example of usage:

new IntervalJob(1000, function() {
    // Operations
});

Pure JS:

setInterval(function() {
    // Operations
}, 1000);

The same code but what if you want to use different interval on each execution step? If your logic in function is not super fast, it spends some time. The setInterval function starts next execution calculation after function is executed. It means that the execution time will be shifted every time.

var executionStartTime = Date.now();

setInterval(function() {
    // Operations which are executed for 20ms
    console.log(Date.now() - executionStartTime);
}, 100);

Look at the console output:

$ 100
$ 220
$ 340
$ 460

etc...

This issue is fixed in IntervalJob.

CronJob

It is easy to realize execution for periodic values. For example, execution every minute or every hour. But if you need to execute a function every month or year, there is no fixed interval value. CronJob solves this problem.

Cron supports sophisticated formats like * h * M * *. It is unnecessary to show examples comparison (it will make this article twice bigger). I'm sure that you are perfect developers and you can implement it but why should you do it if it is done instead of you? Just use CronJob!

Invisible reasons to use Scheduler Cron

I'm worknig on a lot of formats support. They include also some interesting formats which are not supported by standart Cron. Performance is very important. That's why there is an independent issue to make it as fast as possible.

The benefits of using Scheduler Cron are obvious. Download it and use in your projects. Just enjoy.

Updated