Skip to content

Instantly share code, notes, and snippets.

@Hexagon
Last active November 1, 2022 12:44
Show Gist options
  • Save Hexagon/703f85f2dd86443cc17eef8f5cc6cb70 to your computer and use it in GitHub Desktop.
Save Hexagon/703f85f2dd86443cc17eef8f5cc6cb70 to your computer and use it in GitHub Desktop.
// Timer helper
class Timer {
constructor() {
this.t = process.hrtime.bigint();
}
end() {
return Number((process.hrtime.bigint() - this.t)/BigInt(1000))/1000;
}
};
// Run all test cases
runtest("When is next saturday 29th of february","0 0 0 29 2 6");
runtest("When is next 15th of february","0 0 0 15 2 *");
runtest("When is next monday in october","0 0 0 * 10 1");
runtest("When is 23:00 next 31st march","0 0 23 31 3 *");
// Run test using all libraries and specified pattern
function runtest(text, targetPattern) {
console.log("");
console.log("Test: " + text + ", pattern '"+targetPattern+"'");
console.log("");
// node-schedule
const schedule = require("node-schedule");
let nsTime = new Timer();
const job = schedule.scheduleJob(targetPattern, function(){
console.log('This will not run right now');
});
console.log("node-schedule: " + new Date(job.nextInvocation()).toLocaleString() + " in " + nsTime.end() + "ms");
// cronosjs
const cronos = require("cronosjs");
let cjTime = new Timer();
const cjJob = cronos.CronosExpression.parse(targetPattern);
console.log("cronosjs: " + new Date(cjJob.nextDate()).toLocaleString() + " in " + cjTime.end() + "ms");
// node-cron
const nodecron = require("node-cron");
let ncTime = new Timer();
const job2 = nodecron.schedule(targetPattern, function(a,b,c){
console.log('This will not run right now');
});
console.log("node-cron: ???" + " in " + ncTime.end() + "ms");
// cron
const CronJob = require("cron").CronJob;
let cTime = new Timer();
const job3 = new CronJob(targetPattern, function(){
console.log('This will not run right now');
});
console.log("cron: " + new Date(job3.nextDates()).toLocaleString() + " in " + cTime.end() + "ms");
// Croner
const Cron = require("croner");
let cronerTime = new Timer();
const job4 = Cron(targetPattern, { legacyMode: true}, function(){
console.log('This will not run right now');
});
console.log("croner (legacy mode, default): " + job4.next().toLocaleString() + " in " + cronerTime.end() + "ms");
// Croner
const Cron2 = require("croner");
let croner2Time = new Timer();
const job5 = Cron(targetPattern, { legacyMode: false}, function(){
console.log('This will not run right now');
});
console.log("croner (croner mode): " + job5.next().toLocaleString() + " in " + croner2Time.end() + "ms");
}
@Hexagon
Copy link
Author

Hexagon commented Aug 17, 2022

👍

@Hexagon
Copy link
Author

Hexagon commented Nov 1, 2022

Improved version available at https://github.com/hexagon/cron-comparison

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment