Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First pass at performance optimization

  • Loading branch information...
commit c50bf71685ca98d2b54b91d3e3b57fcf2b0f1031 1 parent da5093c
@sstephenson authored
View
70 lib/eco/compiler.coffee
@@ -9,41 +9,47 @@ exports.compile = compile = (source, options) ->
"""
#{identifier} = function(__obj) {
- var _safe = function(value) {
- if (typeof value === 'undefined' && value == null)
- value = '';
- var result = new String(value);
- result.ecoSafe = true;
- return result;
+ if (!__obj) __obj = {};
+ var __out = [], __capture = function(callback) {
+ var out = __out, result;
+ __out = [];
+ callback.call(this);
+ result = __out.join('');
+ __out = out;
+ return __safe(result);
+ }, __sanitize = function(value) {
+ if (value && value.ecoSafe) {
+ return value;
+ } else if (typeof value !== 'undefined' && value != null) {
+ return __escape(value);
+ } else {
+ return '';
+ }
+ }, __safe, __objSafe = __obj.safe, __escape = __obj.escape;
+ __safe = __obj.safe = function(value) {
+ if (value && value.ecoSafe) {
+ return value;
+ } else {
+ if (!(typeof value !== 'undefined' && value != null)) value = '';
+ var result = new String(value);
+ result.ecoSafe = true;
+ return result;
+ }
};
- return (function() {
- var __out = [], __self = this, _print = function(value) {
- if (typeof value !== 'undefined' && value != null)
- __out.push(value.ecoSafe ? value : __self.escape(value));
- }, _capture = function(callback) {
- var out = __out, result;
- __out = [];
- callback.call(this);
- result = __out.join('');
- __out = out;
- return _safe(result);
+ if (!__escape) {
+ __escape = __obj.escape = function(value) {
+ return ('' + value)
+ .replace(/&/g, '&')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/"/g, '&quot;');
};
+ }
+ (function() {
#{indent script, 4}
- return __out.join('');
- }).call((function() {
- var obj = {
- escape: function(value) {
- return ('' + value)
- .replace(/&/g, '&amp;')
- .replace(/</g, '&lt;')
- .replace(/>/g, '&gt;')
- .replace(/"/g, '&quot;');
- },
- safe: _safe
- }, key;
- for (key in __obj) obj[key] = __obj[key];
- return obj;
- })());
+ }).call(__obj);
+ __obj.safe = __objSafe, __obj.escape = __escape;
+ return __out.join('');
};
"""
View
8 lib/eco/preprocessor.coffee
@@ -26,7 +26,7 @@ exports.Preprocessor = class Preprocessor
printString: (string) ->
if string.length
- @record "_print _safe #{sys.inspect string}"
+ @record "__out.push #{sys.inspect string}"
beginCode: (options) ->
@options = options
@@ -35,16 +35,16 @@ exports.Preprocessor = class Preprocessor
if code isnt "end"
if @options.print
if @options.safe
- @record "_print _safe #{code}"
+ @record "__out.push #{code}"
else
- @record "_print #{code}"
+ @record "__out.push __sanitize #{code}"
else
@record code
indent: (capture) ->
@level++
if capture
- @record "_capture #{capture}"
+ @record "__capture #{capture}"
@captures.unshift @level
@indent()
View
22 test/fixtures/capture.coffee
@@ -1,13 +1,13 @@
renderItem = (item) ->
- _capture ->
- _print _safe '\n <div class="item">\n <span class="name">'
- _print item.name
- _print _safe '</span>\n <span class="price">$'
- _print item.price
- _print _safe '</span>\n </div>\n'
-_print _safe '\n\n'
+ __capture ->
+ __out.push '\n <div class="item">\n <span class="name">'
+ __out.push __sanitize item.name
+ __out.push '</span>\n <span class="price">$'
+ __out.push __sanitize item.price
+ __out.push '</span>\n </div>\n'
+__out.push '\n\n'
for item in @items
- _print _safe '\n '
- _print renderItem item
- _print _safe '\n'
-_print _safe '\n'
+ __out.push '\n '
+ __out.push __sanitize renderItem item
+ __out.push '\n'
+__out.push '\n'
View
10 test/fixtures/hello.coffee
@@ -1,5 +1,5 @@
-_print _safe 'Hello, '
-_print @name
-_print _safe '.\nI\'M SHOUTING AT YOU, '
-_print @name.toUpperCase()
-_print _safe '!\n'
+__out.push 'Hello, '
+__out.push __sanitize @name
+__out.push '.\nI\'M SHOUTING AT YOU, '
+__out.push __sanitize @name.toUpperCase()
+__out.push '!\n'
View
30 test/fixtures/helpers.coffee
@@ -1,16 +1,16 @@
for item in @items
- _print _safe '\n '
- _print @contentTag "div", class: "item", =>
- _capture =>
- _print _safe '\n '
- _print @contentTag "span", class: "price", ->
- _capture ->
- _print _safe '$'
- _print item.price
- _print _safe '\n '
- _print @contentTag "span", class: "name", ->
- _capture ->
- _print item.name
- _print _safe '\n '
- _print _safe '\n'
-_print _safe '\n'
+ __out.push '\n '
+ __out.push __sanitize @contentTag "div", class: "item", =>
+ __capture =>
+ __out.push '\n '
+ __out.push __sanitize @contentTag "span", class: "price", ->
+ __capture ->
+ __out.push '$'
+ __out.push __sanitize item.price
+ __out.push '\n '
+ __out.push __sanitize @contentTag "span", class: "name", ->
+ __capture ->
+ __out.push __sanitize item.name
+ __out.push '\n '
+ __out.push '\n'
+__out.push '\n'
View
22 test/fixtures/projects.coffee
@@ -1,14 +1,14 @@
if @projects.length
- _print _safe '\n '
+ __out.push '\n '
for project in @projects
- _print _safe '\n <a href="'
- _print project.url
- _print _safe '">'
- _print project.name
- _print _safe '</a>\n '
- _print _safe project.description
- _print _safe '\n '
- _print _safe '\n'
+ __out.push '\n <a href="'
+ __out.push __sanitize project.url
+ __out.push '">'
+ __out.push __sanitize project.name
+ __out.push '</a>\n '
+ __out.push project.description
+ __out.push '\n '
+ __out.push '\n'
else
- _print _safe '\n No projects\n'
-_print _safe '\n'
+ __out.push '\n No projects\n'
+__out.push '\n'

1 comment on commit c50bf71

@sstephenson

Eco is now the fastest JS template language (at least in Safari and Chrome): http://jsperf.com/dom-vs-innerhtml-based-templating/11

Please sign in to comment.
Something went wrong with that request. Please try again.