Greedy vs lazy regex (v5)

Revision 5 of this benchmark created by Sam Grundman on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div class="ally-tooltip" id="guide-tooltip">
        <div class="tip-content">
                <a href="#" class="tip-close" data-close-tip><i class="icon-check"></i></a>
                <div>We can place <abbr>HTML</abbr> content <em>here</em>.</div>
        </div>
        <span class="tip"></span>
</div>

Setup

$('.ally-tooltip').html('<div class="tip-content"><a href="#" class="tip-close" data-close-tip><i class="icon-check"></i></a><div>We can place <abbr>HTML</abbr> content <em>here</em>.</div></div><span class="tip"></span></div>')

Test runner

Ready to run.

Testing in
TestOps/sec
Greedy
var $at = $('.ally-tooltip')
if ($at.html().indexOf(' ') > -1) {
  var text = $at.html().match(/<.+?>|\S+?(?=<)|\S+/g),
    wraps = text.map(function(item) {
      if (!/<.+?>/g.test(item)) {
        item = '<t-tip>' + item + '</t-tip> ';
      }
      return item;
    }).join('').trim();
  $at.html(wraps);
}
ready
Lazy
var $at = $('.ally-tooltip')
if ($at.html().indexOf(' ') > -1) {
  var text = $at.html().match(/<[^>]+>|\S+?(?=<)|\S+/g),
    wraps = text.map(function(item) {
      if (!/<[^>?]+>/g.test(item)) {
        item = '<t-tip>' + item + '</t-tip> ';
      }
      return item;
    }).join('').trim();
  $at.html(wraps);
}
ready
Lazier
var $at = $('.ally-tooltip')
if ($at.html().indexOf(' ') > -1) {
  var text = $at.html().match(/<[^>]+>|[^\s<]+(?=<)|\S+/g),
    wraps = text.map(function(item) {
      if (!/<[^>?]+>/g.test(item)) {
        item = '<t-tip>' + item + '</t-tip> ';
      }
      return item;
    }).join('').trim();
  $at.html(wraps);
}
ready
Less Regex
var $at = $('.ally-tooltip')
if ($at.html().indexOf(' ') > -1) {
  var text = $at.html().match(/<[^>]+>|\S+?(?=<)|\S+/g),
    wraps = text.map(function(item) {
      if (item.indexOf('<') !== 0 && item.lastIndexOf('>') !== item.length - 1) {
        item = '<t-tip>' + item + '</t-tip> ';
      }
      return item;
    }).join('').trim();
  $at.html(wraps);
}
ready

Revisions

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

  • Revision 5: published by Sam Grundman on