Replace vs Substring (v4)

Revision 4 of this benchmark created on


Description

When the location of a substring that should be replaced is known, what's faster: .replace or .substring?

Preparation HTML

<div id="bob">hi[br]bob</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<script>
var bob_div = $("#bob"),
    bob_div_text = $("#bob").text();

$.fn.text2 = function(text) {
    this.text(text);
    this.html(this.html().split('[br]').join('<br>'));
    return this;
};

$.fn.text3 = function(text) {
    this.text(text);
    this.html(this.html().replace('[br]','<br>'));
    return this;
};
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
substring
bob_div.text2(bob_div_text);
ready
replace
bob_div.text3(bob_div_text);
ready

Revisions

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