Skip to content

Instantly share code, notes, and snippets.

@pwnall
Created November 28, 2013 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwnall/7686635 to your computer and use it in GitHub Desktop.
Save pwnall/7686635 to your computer and use it in GitHub Desktop.
Experiment for proving that Date.now() goes back quite often by quite a bit in V8 on Windows.
<!DOCTYPE html>
<script>
// Watch out for the clock going backwards for 30 seconds.
//
// Browsers will stop our JavaScript if it runs for too long without returning
// control to the event loop.
var smallTest = function() {
var startTime = Date.now();
var oldNow = startTime;
while (true) {
var now = Date.now();
var delta = now - oldNow;
if (delta < 0) {
console.log("Time went backwards by " + (0 - delta));
}
oldNow = now;
if (now - startTime > 30 * 1000) {
break; // Stop after 30 seconds.
}
}
};
// Call smallTest() 10 times, with 10-second breaks in between.
var largeTest = function() {
var runsLeft = 10;
var oneRun = function () {
smallTest();
runsLeft -= 1;
if (runsLeft === 0) {
console.log("Done");
return;
}
setTimeout(oneRun, 10);
};
oneRun();
};
largeTest();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment