Last active
June 8, 2017 17:03
-
-
Save SgtPooki/6937749 to your computer and use it in GitHub Desktop.
Simple test to see how long it takes a random alphanumeric function to generate a duplicate string.
This file contains hidden or 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 will no longer lock up your browser tab, but it will not find a match for a very long time **** | |
***** Do not run this in a tab you do not want to reload **** | |
************************************************************************************************************* | |
* | |
* This was just me playing around trying to see if I could get a duplicate random string | |
* when using the Math.random().toString(radix) method. | |
* | |
* I had planned to implement web workers with this as well so that I could let it | |
* keep running forever and just see how long it took to get a duplicate string, but setTimout and setInterval work just fine... | |
*/ | |
var radix = 32; | |
var getRandomString = function() { return Math.random().toString(radix).slice(2); }; | |
var stringToMatch = getRandomString(); | |
window.matchesArray = []; | |
window.matchesTried = 0; | |
var startTime = performance.now(); | |
var generatedStrings = []; | |
var timeElapsed = 0; | |
var endTime; | |
var newStringInterval; | |
var matchCheckInterval = 1000; | |
var generateNewString = function generateNewString() { | |
generatedStrings.push(getRandomString()); | |
}; | |
var checkForMatches = function checkForMatches() { | |
endTime = performance.now() - startTime; | |
if (generatedStrings.indexOf(stringToMatch) === -1) { | |
matchesTried += generatedStrings.length - 1; | |
generatedStrings = []; | |
console.log('no match found from ' + matchesTried + ' different matches in ' + (endTime/1000)/60 + ' minutes.'); | |
return setTimeout(checkForMatches, matchCheckInterval); | |
} else { | |
console.log('Matched on ' + stringToMatch + ' in ' + (endTime/1000)/60 + ' minutes'); | |
} | |
}; | |
newStringInterval = setInterval(generateNewString, 0); | |
setTimeout(checkForMatches, matchCheckInterval); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment