String.slice vs string to array and join

Edit on GitHub

Setup

var cc = "Nananananananananananananananana Batman!"

Tests

  • String.slice

    var cc = "Nananananananananananananananana Batman!"
    return cc.slice(0, -4).replace(/./g, '#') + cc.slice(-4);
  • string to array and join

    var cc = "Nananananananananananananananana Batman!"
    if (cc.length <= 4){
        return cc;
      }
      else{
        var stringArr = cc.split("");
        for (var i = 0; i < stringArr.length-4; i++){
          stringArr[i] = "#";
        }
        return stringArr.join("");
      }