Replace vs Substring

Benchmark created by Andrew Petersen on


Preparation HTML

<script>
  function regEscape(str) {
   return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  }
  
  var longString = '<p><b>JavaScript</b> is an <a href="/wiki/Implementation" title="Implementation">implementation</a> of the <a href="/wiki/ECMAScript" title="ECMAScript">ECMAScript</a> language standard and is typically used to enable <a href="/wiki/Computer_programming" title="Computer programming">programmatic</a> access to computational objects within a host environment. It can be characterized as a <a href="/wiki/Prototype-based" title="Prototype-based" class="mw-redirect">prototype-based</a> <a href="/wiki/Object-oriented_programming" title="Object-oriented programming">object-oriented</a><sup id="cite_ref-ECMA-262_5-0" class="reference"><a href="#cite_note-ECMA-262-5"><span>[</span>6<span>]</span></a></sup> <a href="/wiki/Scripting_language" title="Scripting language">scripting language</a> that is <a href="/wiki/Dynamic_language" title="Dynamic language" class="mw-redirect">dynamic</a>, <a href="/wiki/Weak_typing" title="Weak typing">weakly typed</a> and has <a href="/wiki/First-class_function" title="First-class function">first-class functions</a>. It is also considered a <a href="/wiki/Functional_programming" title="Functional programming">functional programming</a> language<sup id="cite_ref-jsfunc_0-1" class="reference"><a href="#cite_note-jsfunc-0"><span>[</span>1<span>]</span></a></sup> like <a href="/wiki/Scheme_(programming_language)" title="Scheme (programming language)">Scheme</a> and <a href="/wiki/OCaml" title="OCaml" class="mw-redirect">OCaml</a> because it has <a href="/wiki/Closure_(computer_science)" title="Closure (computer science)" class="mw-redirect">closures</a> and supports <a href="/wiki/Higher-order_function" title="Higher-order function">higher-order functions</a>.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span>[</span>7<span>]</span></a></sup></p>';
  
  var copy;
  
  var toFind = '<b>JavaScript</b> is an <a href="/wiki/Implementation" title="Implementation">implementation</a> of the <a href="/wiki/ECMAScript" title="ECMAScript">ECMAScript</a> language standard and is typically used to enable <a href="/wiki/Computer_programming" title="Computer programming">programmatic</a> access to computational objects within a host environment.';
  
  var toFindReg = new RegExp('(' + regEscape(toFind) + ')', 'gi');
  var tempResult = toFindReg.exec(longString);
  var start = tempResult.index;
  
  var result;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
substring
copy = longString;
result = copy.substring(0, start) + '<wrap>' + copy.substring(start, start + toFind.length) + '</wrap>' + copy.substring(start + toFind.length);
ready
replace
copy = longString;
result = copy.replace(toFindReg, '<wrap>$1</wrap>');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.