jweinst1
2/25/2017 - 7:20 PM

jsstringreplaceetest.js

//timed string replacement

var test = "Hello World!";
var s;
console.time("str method");
for(var i=10000;i>0;i--) s = test.replace(test.charAt(2), "P");
console.timeEnd("str method");
//9-10ms

console.time("arr method");
for(var i=10000;i>0;i--) {s = test.split(""); s[2] = "P"; s = s.join("")}
console.timeEnd("arr method");

//17-20ms

//native string methods ae faster