Last active
September 21, 2016 10:09
-
-
Save sseletskyy/48dbbc6228cf6f4bb7f25d80fb7a7d59 to your computer and use it in GitHub Desktop.
quick performance test of the function in js
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
var testF = function(func, args){ | |
var counter = 100000, i = counter, startTimer, endTimer; | |
startTimer = performance.now(); | |
for(;i>0;i--) { | |
func.apply(null, args); | |
} | |
endTimer = performance.now(); | |
return (endTimer - startTimer) / counter; | |
} | |
var encrypt = function encrypt(text, n) { | |
if (!text || text.length === 0 || n <= 0) return text; | |
var index = 1, textLen = text.length, firstPart = [], secondPart = []; | |
while (index < textLen) { | |
firstPart.push(text[index]); | |
secondPart.push(text[index-1]); | |
index += 2; | |
} | |
if (index === textLen) { | |
secondPart.push(text[textLen-1]); | |
} | |
return encrypt( firstPart.join('') + secondPart.join(''), n - 1); | |
} | |
var testString = "ABCDEFGHIJKLMNO123456789ABCDEFGHIJKLMNO123456789"; | |
var result = testF(encrypt, [testString, 10]); | |
$('body').html("Everage time: " + result + " milliseconds."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment