Script Preparation code:
x
 
var str = 'fred red bed said', searchTerm = 'bed',
    arr = str.split(' ');
function removeSubstr(str, substr){
    tmp = " " + str + " ";
    substr = " " + substr + " ";
    if (tmp.indexOf(substr) !== -1 ) {
        tmp = tmp.replace(substr, "").trim(); 
        return tmp
    }
  return str;
}
function removeSubstr2(str, substr){
    var len = str.length, tmp = (" " + str + " ").replace(" " + substr + " ", "").trim(); 
    if ( str.length < len ) return tmp;
    return str;
}
function removeSubstr3(str, substr){
  var strSpl = str.split(' '), idx = strSpl.indexOf(substr);
  if ( idx > -1) {
    return strSpl.splice(idx, 1).join(' ');
  }
  return str;
}
function removeElem(arr, substr){
  var idx = arr.indexOf(substr);
  if ( idx > -1) {
    return arr.splice(idx, 1);
  }
  return arr;
}
Tests:
  • String Replace No Compare

     
    removeSubstr(str, searchTerm)
  • String Replace With Compare

     
    removeSubstr2(str, searchTerm)
  • Convert to Array

     
    removeSubstr3(str, searchTerm)
  • Remove From Array

     
    removeElem(arr, searchTerm)
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    String Replace No Compare
    String Replace With Compare
    Convert to Array
    Remove From Array

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 8 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Chrome 52 on Windows
View result in a separate tab
Test name Executions per second
String Replace No Compare 179087.3 Ops/sec
String Replace With Compare 238998.1 Ops/sec
Convert to Array 124368.9 Ops/sec
Remove From Array 903509.2 Ops/sec