Split vs replace

JavaScript performance comparison

Test case created by Josh and last updated

Info

testing a series of replaces to a replace and a split

Preparation code

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

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Serise of replaces
xml = "<Question><Text>IsmCflQuantity15wTwist</Text><FieldName>IsmCflQuantity15wTwist</FieldName><ValueType>String</ValueType><Answer>Avalue</Answer></Question>";
val = xml.replace(/(<Question>|<\/Question>)/g, "");
val = val.split(/(<Text>|<\/Text>|<\/FieldName>|<FieldName>|<ValueType>|<\/ValueType>|<Answer>|<\/Answer>)/g);
var a = "";
var b = "";
var c = "";
var d = "";
for (var i = 0; i < val.length; i++) {
  var item = val[i];
  if (item === "<Text>") {
    a = val[++i];
    i++;
    continue;
  }
  if (item === "<FieldName>") {
    b = val[++i];
    i++;
    continue;
  }
  if (item === "<ValueType>") {
    c = val[++i];
    i++;
    continue;
  }
  if (item === "<Answer>") {
    d = val[++i];
    i++;
    continue;
  }
}
pending…
Jquery
xml = "<Question><Text>IsmCflQuantity15wTwist</Text><FieldName>IsmCflQuantity15wTwist</FieldName><ValueType>String</ValueType><Answer>Avalue</Answer></Question>";

var a = $(xml).find("Text").text();
var b = $(xml).find("FieldName").text();
var c = $(xml).find("ValueType").text();
var d = $(xml).find("Answer").text();
pending…

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

Compare results of other browsers

0 comments

Add a comment