Last active
May 20, 2024 20:35
-
-
Save astrarudra/a357ed2a07e11fddcb333e9eadf04dff to your computer and use it in GitHub Desktop.
This script checks the version of a JSON response from a gist raw API endpoint every 500 milliseconds to find out the update efficency
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
/** | |
* This script checks the version of a JSON response from a gist raw API endpoint every 500 milliseconds to find out the update efficency. | |
* Run this exactly update clicking update JSON, check my test.json jist. Clone it and modify the version. | |
* If the version is X, it logs the data. If the version is not X, it logs the current time, the version, | |
* and the time difference between the request initiation and the response reception. | |
* The script also ensures that the API response is not cached by appending a unique timestamp | |
* to each request URL and setting cache control options. | |
*/ | |
let intervalId; | |
let a = new Date(); | |
const checkVersion = async () => { | |
try { | |
// Append the current timestamp to the URL to prevent caching | |
const response = await fetch(`https://gist.githubusercontent.com/astrarudra/c9a7783d2a4a334a192235d762a27c0d/raw?timestamp=${(new Date()).getTime()}`, { | |
cache: "no-store" // Also try to prevent cache on fetch request | |
}); | |
const data = await response.json(); | |
if (data.version === 12) { | |
console.log(data); | |
} else { | |
const b = new Date(); | |
const timeDifference = b - a; | |
console.log(b, data.version, `Time Difference: ${timeDifference}ms`); | |
// Clear the interval to stop the repeated execution | |
clearInterval(intervalId); | |
} | |
} catch (error) { | |
console.error('Error fetching the data:', error); | |
} | |
}; | |
// Ping the URL every second (1000 ms) | |
intervalId = setInterval(checkVersion, 500); | |
/* | |
From my testing - Run around 5 times | |
The latency varied from 20s to 50s. So <1min | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment