filter tag from string

JavaScript performance comparison

Test case created by neizod

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  // ====== commons constant ======
  var arrayTag, tagBarRef, tagFooRef, tagGetID, tagGetKw, tagSPAN;
  
  tagFooRef = "<span class='FooRef'>";
  tagBarRef = "<span class='BarRef'>";
  tagGetID = "<span class='GetID'>";
  tagGetKw = "<span class='GetKw'>";
  tagSPAN = "</span>";
  
  arrayTag = [tagFooRef, tagBarRef, tagGetID, tagGetKw];
  
  
  
  // ============ test ============
  var inputs = [
    "Hello world! <span class='FooRef'>DMR</span>",
    "This year on <span class='GetKw Preserve'>Learn Station 4</span> isn't easy as it seen. <span class='FooRef'>Abcd</span> <span class='BarRef'>The Newx</span>",
    "<span class='GetKw Preserve'>Burial at Me</span> release! <span class='GetKw'>ChemShock Infinite</span> <span class='GetID'>ChemShock</span> <span class='BarRef'>Game Mang</span>",
    "This paper has very long reference. <span class='BarRef'>Lee CPeew</span> <span class='BarRef'>Doraejin</span> <span class='BarRef'>Haiso</span> <span class='BarRef'>Amhart</span> <span class='BarRef'>Elite</span> <span class='BarRef'>Pawon Pawon</span> <span class='BarRef'>Flirt Love Trust</span> <span class='FooRef'>Cookie Coffee Run Into Fuji Forest</span> <span class='FooRef'>Blog No Name</span> <span class='FooRef'>Million Share Billion Like</span> <span class='FooRef'>Pan Tup</span> <span class='FooRef'>Twister</span>"
  ];
  
  function test(f) {
    for (var i = 0; i < inputs.length; i++) {
      f(inputs[i]);
    }
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Recursive filter
// CoffeeScript
var filterTag, removeTag;

filterTag = function(msgStr, filterStr) {
  var indexSpan, indexStart, tmp;
  indexStart = -1;
  indexSpan = -1;
  tmp = '';
  indexStart = msgStr.indexOf(filterStr);
  if (indexStart !== -1) {
    indexSpan = msgStr.indexOf(tagSPAN, indexStart);
    tmp = msgStr.slice(indexStart, indexSpan + 7);
    msgStr = msgStr.replace(tmp, '');
    return filterTag(msgStr, filterStr);
  } else {
    return msgStr;
  }
};

removeTag = function(msgStr) {
  var tagRemove, _i, _len;
  for (_i = 0, _len = arrayTag.length; _i < _len; _i++) {
    tagRemove = arrayTag[_i];
    msgStr = filterTag(msgStr, tagRemove);
  }
  return msgStr;
};

test(removeTag);
pending…
Improved filter: remember search index until now.
// CoffeeScript
var filterTag, removeTag;

filterTag = function(msgStr, filterStr, searchFrom) {
  var indexSpan, indexStart, tmp;
  if (searchFrom == null) {
    searchFrom = 0;
  }
  indexStart = -1;
  indexSpan = -1;
  tmp = '';
  indexStart = msgStr.indexOf(filterStr, searchFrom);
  if (indexStart !== -1) {
    indexSpan = msgStr.indexOf(tagSPAN, indexStart);
    tmp = msgStr.slice(indexStart, indexSpan + 7);
    msgStr = msgStr.replace(tmp, '');
    return filterTag(msgStr, filterStr, indexStart);
  } else {
    return msgStr;
  }
};

removeTag = function(msgStr) {
  var tagRemove, _i, _len;
  for (_i = 0, _len = arrayTag.length; _i < _len; _i++) {
    tagRemove = arrayTag[_i];
    msgStr = filterTag(msgStr, tagRemove);
  }
  return msgStr;
};

test(removeTag);
pending…
Improved filter: direct slice result instead of remember to tmp.
// CoffeeScript
var filterTag, removeTag;

filterTag = function(msgStr, filterStr, searchFrom) {
  var indexSpan, indexStart;
  if (searchFrom == null) {
    searchFrom = 0;
  }
  indexStart = -1;
  indexSpan = -1;
  indexStart = msgStr.indexOf(filterStr, searchFrom);
  if (indexStart !== -1) {
    indexSpan = msgStr.indexOf(tagSPAN, indexStart);
    msgStr = msgStr.slice(0, indexStart) + msgStr.slice(indexSpan + 7);
    return filterTag(msgStr, filterStr, indexStart);
  } else {
    return msgStr;
  }
};

removeTag = function(msgStr) {
  var tagRemove, _i, _len;
  for (_i = 0, _len = arrayTag.length; _i < _len; _i++) {
    tagRemove = arrayTag[_i];
    msgStr = filterTag(msgStr, tagRemove);
  }
  return msgStr;
};

test(removeTag);
pending…
Improved filter: use loop instead of recursive
// CoffeeScript
var filterTag, removeTag;

filterTag = function(msgStr, filterStr) {
  var indexSpan, indexStart;
  indexStart = msgStr.indexOf(filterStr);
  indexSpan = -1;
  while (indexStart !== -1) {
    indexSpan = msgStr.indexOf(tagSPAN, indexStart);
    msgStr = msgStr.slice(0, indexStart) + msgStr.slice(indexSpan + 7);
    indexStart = msgStr.indexOf(filterStr, indexStart);
  }
  return msgStr;
};

removeTag = function(msgStr) {
  var tagRemove, _i, _len;
  for (_i = 0, _len = arrayTag.length; _i < _len; _i++) {
    tagRemove = arrayTag[_i];
    msgStr = filterTag(msgStr, tagRemove);
  }
  return msgStr;
};

test(removeTag);
pending…
jQuery style
function removeTag(s) {
  var el = $('<div>').html(s);
  el.find('span')
    .filter('.FooRef,.BarRef,.GetKw,.GetID')
    .not('.Preserve')
    .remove();
  return el.html();
}

test(removeTag);
pending…
Regex style
function removeTag(s) {
  return s.split(/<span class='(?:FooRef|BarRef|GetKw|GetID)'>.*?<\/span>/).join('');
}

test(removeTag);
pending…

Compare results of other browsers

Revisions

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

0 Comments