JSFiddle - React, Tailwind, and code Playground
JavaScript
/*
* Returns a String with length = 2^n whose characters are 'x'
*/
function getString(n) {
var str = 'x';
for (var i = 0; i < n; i++)
str = str + str;
return str
}
/*
* Returns the time of comparing two equals strings with length 2^n using the === operator
*/
function compare(str1, str2) {
equals = str1 === str2;
// equals is always true. we assign the result of the comparaton to a variable because if we don't do it, javascript optimizations could skip the execution of the comparation instruction (as it wouldn't have any side effects)
return equals;
}
function main() {
// We double the string length every iteration
for (var n = 1; n <= 27; n++) {
var str1 = getString(n);
var str2 = getString(n);
var t0 = Date.now();
var result = compare(str1,str2);
var t1 = Date.now() - t0;
console.log("lengths: "+str1.length+",result: "+result);
console.log("time elapsted: "+ Math.pow(2, n) + ': ' + t1);
}
}
main();