Skip to content

Instantly share code, notes, and snippets.

@jafar-jabr
Last active February 27, 2022 12:53
Show Gist options
  • Save jafar-jabr/47d5ed79b971be92dcbb84e118dcdb20 to your computer and use it in GitHub Desktop.
Save jafar-jabr/47d5ed79b971be92dcbb84e118dcdb20 to your computer and use it in GitHub Desktop.
hijriToCalendars versions performance compare
const performanceCompare = (callBack1, callBack2, amplifier=1000000) => {
const t1 = performance.now();
callBack1();
const t2 = performance.now();
const t3 = performance.now();
callBack2();
const t4 = performance.now();
const perform1 = ~~(t2-t1);
const perform2 = ~~(t4-t3);
if(perform1 || perform2) {
return `${callBack1} took ${perform1} milliseconds while ${callBack2} took ${perform2} milliseconds`
}
const t5 = performance.now();
for(let i = 0; i < amplifier; i+=1) {
callBack1();
}
const t6 = performance.now();
const t7 = performance.now();
for(let j = 0; j < amplifier; j+=1) {
callBack2();
}
const t8 = performance.now();
const perform3 = ~~(t5-t6);
const perform4 = ~~(t8-t7);
return `iterating ${amplifier} times ${callBack1} took ${perform3} milliseconds while ${callBack2} took ${perform4} milliseconds`
}
function hijriToCalendars(year, month, day, op={}) {
op.fromCal ??= "islamic-umalqura"; //
let gD = new Date(Date.UTC(2000,0,1));
gD = new Date(gD.setUTCDate(gD.getUTCDate() +
~~(227022+(year+(month-1)/12+day/354)*354.367)));
const gY = gD.getUTCFullYear(gD)-2000,
dFormat = new Intl.DateTimeFormat('en-u-ca-' + op.fromCal, {dateStyle:'short', timeZone:'UTC'});
gD = new Date(( gY < 0 ? "-" : "+")+("00000" + Math.abs(gY)).slice(-6)+"-"+("0" + (gD.getUTCMonth(gD)+1)).slice(-2)+"-" + ("0" + gD.getUTCDate(gD)).slice(-2));
let [iM,iD,iY]= [...dFormat.format(gD).split("/")], i=0;
gD = new Date(gD.setUTCDate(gD.getUTCDate() +
~~(year*354+month*29.53+day-(iY.split(" ")[0]*354+iM*29.53+iD*1)-2)));
while (i < 4) {
[iM,iD,iY] = [...dFormat.format(gD).split("/")];
if (iD == day && iM == month && iY.split(" ")[0] == year) return formatOutput(gD);
gD = new Date(gD.setUTCDate(gD.getUTCDate()+1)); i++;
}
throw new Error("Invalid "+op.fromCal+" date!");
function formatOutput(gD){
return "toCal"in op ? (op.calendar= op.toCal,
new Intl.DateTimeFormat(op.locale ??= "en", op).format(gD)) : gD;
}
}
function hijriToCalendarsv2(year, month, day, op={}) {
const { fromCal ="islamic-umalqura", toCal=null, locale="en" } = op;
let gD = new Date(Date.UTC(2000, 0, 1))
gD.setUTCDate(gD.getUTCDate() + ~~(227022+(year+(month-1)/12+day/354)*354.367));
const dFormat = new Intl.DateTimeFormat(`en-u-ca-${fromCal}`, { dateStyle:'short', timeZone:'UTC' });
const gY = gD.getUTCFullYear()-2000;
gD = new Date(`${gY < 0 ? "-" : "+"}${`00000${Math.abs(gY)}`.slice(-6)}-${`0${gD.getUTCMonth()+1}`.slice(-2)}-${`0${gD.getUTCDate()}`.slice(-2)}`);
let [iM, iD, iY] = dFormat.format(gD).split("/");
gD.setUTCDate(gD.getUTCDate() + ~~(year*354+month*29.53+day-(iY.split(" ")[0]*354+iM*29.53+iD*1)-2));
const formatOutput = (_gD) => {
return toCal ? new Intl.DateTimeFormat(locale, op).format(_gD) : _gD;
}
for (let i=0; i < 4; i+=1) {
[iM, iD, iY] = dFormat.format(gD).split("/");
if (~~iD === ~~day && ~~iM === ~~month && ~~iY.split(" ")[0] === ~~year) return formatOutput(gD);
gD.setUTCDate(gD.getUTCDate()+1);
}
throw new Error(`Invalid ${fromCal} date!`);
}
const check = performanceCompare(() => hijriToCalendars(8443,9,1,{}), () => hijriToCalendarsv2(8443,9,1,{}))
console.warn(`check ${check}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment