unescape

Benchmark created on


Description

revertHtmlEntities

Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

Setup

function revertHtmlEntities(str) {
  return String(str)
    .replace(/&amp;/g, '&')
    .replace(/&lt;/g, '<')
    .replace(/&gt;/g, '>')
    .replace(/&quot;/g, '"');
}

function basePropertyOf(object) {
    return function(key) {
      return object == null ? undefined : object[key];
    };
  }

var htmlUnescapes = {
    '&amp;': '&',
    '&lt;': '<',
    '&gt;': '>',
    '&quot;': '"',
    '&#39;': "'"
  };

var reEscapedHtml = /&(?:amp|lt|gt|quot|#39);/g,
      reUnescapedHtml = /[&<>"']/g,
      reHasEscapedHtml = RegExp(reEscapedHtml.source),
      reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
var unescapeHtmlChar = basePropertyOf(htmlUnescapes);

function revertHtmlEntities2(str) {
  const string = String(str); 
  return (string && reHasEscapedHtml.test(string))
        ? string.replace(reEscapedHtml, unescapeHtmlChar)
        : string;
}

Test runner

Ready to run.

Testing in
TestOps/sec
lodash/unescape
_.unescape('Hello &amp; Welcome to &quot;Testing&quot;');
ready
revertHtmlEntities
revertHtmlEntities('Hello &amp; Welcome to &quot;Testing&quot;');
ready
lodash/unescape (noop)
_.unescape('Hello & Welcome to "Testing"');
ready
revertHtmlEntities (noop)
revertHtmlEntities('Hello & Welcome to "Testing"');
ready
revertHtmlEntities2
revertHtmlEntities2('Hello &amp; Welcome to &quot;Testing&quot;');
ready
revertHtmlEntities2 (noop)
revertHtmlEntities2('Hello & Welcome to "Testing"');
ready

Revisions

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