Last active
November 1, 2022 12:44
-
-
Save Hexagon/703f85f2dd86443cc17eef8f5cc6cb70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍