Optimize daysSince2000
10% performance improvement in https://scratch.mit.edu/projects/527835108/
This commit is contained in:
1 file changed
+4
-11
@@ -330,21 +330,14 @@ const timer = () => {
|
||||
return t;
|
||||
};
|
||||
|
||||
// This is the "epoch" for the daysSince2000() function.
|
||||
// Storing this in a variable is faster than constantly recreating it.
|
||||
const daysSince2000Epoch = new Date(2000, 0, 1);
|
||||
|
||||
/**
|
||||
* Returns the amount of days since January 1st, 2000.
|
||||
* @returns {number} Days since 2000.
|
||||
*/
|
||||
const daysSince2000 = () => {
|
||||
const today = new Date();
|
||||
const dstAdjust = today.getTimezoneOffset() - daysSince2000Epoch.getTimezoneOffset();
|
||||
let mSecsSinceStart = today.valueOf() - daysSince2000Epoch.valueOf();
|
||||
mSecsSinceStart += ((today.getTimezoneOffset() - dstAdjust) * 60 * 1000);
|
||||
return mSecsSinceStart / (24 * 60 * 60 * 1000);
|
||||
};
|
||||
const daysSince2000 = () =>
|
||||
// Date.UTC(2000, 0, 1) === 946684800000
|
||||
// Hardcoding it is marginally faster
|
||||
(Date.now() - 946684800000) / (24 * 60 * 60 * 1000);
|
||||
|
||||
/**
|
||||
* Determine distance to a sprite or point.
|
||||
|
||||
Reference in new issue
Block a user