Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0
Firefox 114
Windows
Desktop
one year ago
Test name Executions per second
Outside, literal 90.7 Ops/sec
Inside, literal 96.2 Ops/sec
Outside, object 114.6 Ops/sec
Inside, object 58.9 Ops/sec
Script Preparation code:
AخA
 
const SIZE = 100000;
var chars = new Array(SIZE);
for (let index = 0; index < SIZE; index++) {
  chars[index] = String.fromCodePoint(
    Math.floor( Math.random() * 0x80 )
  );
}
var newChars = new Array(SIZE);
Tests:
  • Outside, literal

     
    const regexp = /[\x00-\x1f\x7f]/g;
    for (const char of chars) {
      newChars.push(char.replace(regexp, ""));
    }
    console.log(newChars);
  • Inside, literal

     
    for (const char of chars) {
      newChars.push(char.replace(/[\x00-\x1f\x7f]/g, ""));
    }
    console.log(newChars);
  • Outside, object

     
    const regexp = new RegExp("[\x00-\x1f\x7f]");
    for (const char of chars) {
      newChars.push(char.replace(regexp, ""));
    }
    console.log(newChars);
  • Inside, object

     
    for (const char of chars) {
      newChars.push(char.replace(new RegExp("[\x00-\x1f\x7f]"), ""));
    }
    console.log(newChars);