jade-escape

Benchmark created by asd on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jade/1.10.0/jade.min.js"></script>
<script>
var locals =   {
    things: [
      { a: 'blah', b: 'halb' }
    , { a: 'clah', b: 'halc' }
    , { a: 'clah', b: 'halc' }
    , { a: 'blah', b: 'halb' }
    , { a: 'clah', b: 'halc' }
    , { a: 'clah', b: 'halc' }
    , { a: 'clah', b: 'halc' }
    , { a: 'blah', b: 'halb' }
    , { a: 'blah', b: 'halb' }
    , { a: 'clah', b: 'halc' }
    , { a: 'blah', b: 'halb' }
    ]
  }

var _ENCODE_HTML_RULES = {
      '&': '&amp;'
    , '<': '&lt;'
    , '>': '&gt;'
    , '"': '&#34;'
    , "'": '&#39;'
    }
  , _MATCH_HTML = /[&<>\'"]/g;

function betterEscape(html){
  return html == undefined
    ? ''
    : String(html)
        .replace(_MATCH_HTML, function(m) {
          return _ENCODE_HTML_RULES[m] || m;
        });
};

var jadeWithBetterEscape = {
  escape: betterEscape
};

var jadeWithCurrentEscape = {
  escape: jade.runtime.escape
};

function jadeOriginal(locals, jade){
var buf = [];
var jade_mixins = {};
var jade_interp;
;var locals_for_with = (locals || {});(function (things, undefined) {
buf.push("<!DOCTYPE html><html><head><title>Fortunes</title></head><body><p>Interesting website</p>");
// iterate things
;(function(){
  var $$obj = things;
  if ('number' == typeof $$obj.length) {

    for (var $index = 0, $$l = $$obj.length; $index < $$l; $index++) {
      var thing = $$obj[$index];

buf.push("<p>" + (jade.escape(null == (jade_interp = thing.a) ? "" : jade_interp)) + "</p><p>" + (jade.escape(null == (jade_interp = thing.b) ? "" : jade_interp)) + "</p>");
    }

  } else {
    var $$l = 0;
    for (var $index in $$obj) {
      $$l++;      var thing = $$obj[$index];

buf.push("<p>" + (jade.escape(null == (jade_interp = thing.a) ? "" : jade_interp)) + "</p><p>" + (jade.escape(null == (jade_interp = thing.b) ? "" : jade_interp)) + "</p>");
    }

  }
}).call(this);

buf.push("</body></html>");}.call(this,"things" in locals_for_with?locals_for_with.things:typeof things!=="undefined"?things:undefined,"undefined" in locals_for_with?locals_for_with.undefined:typeof undefined!=="undefined"?undefined:undefined));;return buf.join("");
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Jade with its current escape function
jadeOriginal(locals, jadeWithCurrentEscape)
ready
Jade with an optimized escape function
jadeOriginal(locals, jadeWithBetterEscape)
ready

Revisions

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