Lotus-templating-alternatives

JavaScript performance comparison

Test case created by Joshua T Kalis

Info

Compare execution times with/out tokenizing of newline and indentation.

Preparation code

<script>
window.lotus_without=function(){var a=function(a,b){return a.slice(b).match(/\{([^\/\}]+)\}(?:(.*?)(?:\{else\}(.*?))?\{\/\1\})?/)},b=function(c,d,e){var f,g,h=a(c),i=0,j="",k;if(!h){return c}else if(h[1]){e=e||[];while(h){k=resolve(h[1],d,e);if(k){if(k.toString()===k.valueOf()){j=k}else{if(h[2].indexOf("{")>=0){if({}.toString.call(k)==="[object Array]"){g=k.length;for(f=0;f<g;f++){j+=k[f].toString()===k[f].valueOf()?h[2].replace(/\{item\}/g,k[f]):b(h[2],k[f])}}else{e.push(h[1]);j=b(h[2],d,e);e.pop()}}else{j=h[2]}}}else{j=h[3]}i=+c.indexOf(h[0]);c=c.slice(0,i)+c.slice(i).replace(h[0],j||"");i+=+(j||"").length;j="";h=a(c,i)}return c}};resolve=function(a,b,c){var d=0,e=0,f=c.slice(0),g;[].push.apply(f,a.split("."));e=f.length;g=b[f[d++]];a=f[e-1];for(;d<e;d++){g=g[f[d]]}if(!g){f.pop();while(f.length&&!g){f.pop();g=resolve(a,b,f)}}return g||""};return function(a,c){return b(a,c)}}();

window.lotus_with=function(){var a=function(a,b){return a.slice(b).match(/\{([^\/\}]+)\}(?:(.*?)(?:\{else\}(.*?))?\{\/\1\})?/)},b=function(c,d,e){var f,g,h=a(c),i=0,j="",k;if(!h){return c}else if(h[1]){e=e||[];while(h){k=resolve(h[1],d,e);if(k){if(k.toString()===k.valueOf()){j=k}else{if(h[2].indexOf("{")>=0){if({}.toString.call(k)==="[object Array]"){g=k.length;for(f=0;f<g;f++){j+=k[f].toString()===k[f].valueOf()?h[2].replace(/\{item\}/g,k[f]):b(h[2],k[f])}}else{e.push(h[1]);j=b(h[2],d,e);e.pop()}}else{j=h[2]}}}else{j=h[3]}i=+c.indexOf(h[0]);c=c.slice(0,i)+c.slice(i).replace(h[0],j||"");i+=+(j||"").length;j="";h=a(c,i)}return c}};resolve=function(a,b,c){var d=0,e=0,f=c.slice(0),g;[].push.apply(f,a.split("."));e=f.length;g=b[f[d++]];a=f[e-1];for(;d<e;d++){g=g[f[d]]}if(!g){f.pop();while(f.length&&!g){f.pop();g=resolve(a,b,f)}}return g||""};return function(a,c){return b(a.replace(/\n+/g,"~n~").replace(/^\s+/g,"~t~"),c).replace(/~n~/g,"\n").replace(/~t~/g,"    ")}}();
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
With tokenizing
window.lotus_with("{name}{last}, {first}{/name}", {name: {first: "Joshua", last: "Kalis"}});
pending…
Without tokenizing
window.lotus_without("{name}{last}, {first}{/name}", {name: {first: "Joshua", last: "Kalis"}});
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