jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
When the location of a substring that should be replaced is known, what's faster: .replace or .substring?
<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>
Ready to run.
Test | Ops/sec | |
---|---|---|
substring |
| ready |
replace |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.