Giter Site home page Giter Site logo

Comments (6)

TimothyGu avatar TimothyGu commented on August 23, 2024

(BTW you don't need the eval())

Will fix.

from ejs.

TimothyGu avatar TimothyGu commented on August 23, 2024

Technically this is a breaking change. @mde what do you think?

To fix this, I would need to make EJS output templates like one of these:

  1. EJS 1 style:

    var buf = []
    [...]
    ;buf.push(data.test)
    [...]
    ;return buf.join('')
  2. Ternary operator:

    var __output = ''
    [...]
    ;__output += (data.test) == null ? '' : data.test
    [...]
    ;return __output

I'll do some benchmark on these.

from ejs.

TimothyGu avatar TimothyGu commented on August 23, 2024
ejs v1 comp x 24,390 ops/sec ±1.21% (95 runs sampled)
ejs v2 comp orig (__output) x 7,175 ops/sec ±0.57% (97 runs sampled)
ejs v2 comp buf x 7,111 ops/sec ±0.33% (101 runs sampled)
ejs v2 comp __output x 6,927 ops/sec ±0.39% (103 runs sampled)

ejs v1 x 344,302 ops/sec ±0.55% (100 runs sampled)
ejs v1 !_with x 1,560,845 ops/sec ±0.49% (96 runs sampled)
ejs v2 orig x 143,544 ops/sec ±0.52% (100 runs sampled)
ejs v2 orig !_with x 1,453,962 ops/sec ±0.67% (97 runs sampled)
ejs v2 buf x 192,506 ops/sec ±0.97% (100 runs sampled)
ejs v2 buf !_with x 1,328,785 ops/sec ±1.62% (92 runs sampled)
ejs v2 out x 131,761 ops/sec ±1.15% (94 runs sampled)
ejs v2 out !_with x 1,457,213 ops/sec ±3.15% (88 runs sampled)
  <% for (var i = 0; i < data.length; i++) { %>
    <% var objects = data[i] %>
    <h1><%= data[i].title %></h1>
    <% for (var j = 0; j < objects.length; j++) { %>
      <% var obj = objects[j] %>
      <% if (obj.passed) { %>
        <p>Passed: <%= obj.name %></p>
        <p>Value: <%= obj.value %></p>
      <% } else { %>
        <p>Failed: <%= obj.name %></p>
        <p>Value: <%= obj.value %></p>
      <% } %>
    <% } %>
  <% } %>
    { data: [
      {
        title: "Test title",
        objects: [
          {
            name: 'this',
            value: 21032,
            passed: false
          }, {
            name: 'test',
            value: 0,
            passed: true
          }
        ]
      }, {
        title: "Another data",
        objects: [
          {
            name: 'that',
            value: 1012021,
            passed: true
          }, {
            name: 'tset',
            value: -2,
            passed: false
          }
        ]
      }
    ]}

According to the principle of the least slow I think if we are going to make this change it should use var buf = [] (the original EJS <=1 approach). I am still surprised how much slower EJS 2 is compared to EJS 1. It might be because of EJS 2's length compared to EJS 1 (~550 vs ~370).

from ejs.

TimothyGu avatar TimothyGu commented on August 23, 2024

Now I read more and more EJS 1 code, I really think it's fairly reusable as EJS v2. The OO stuff and expensive regex matching is really dragging the compilation speed down. EJS v1 depends purely on indexOf which, believe it or not, is fast.

from ejs.

mde avatar mde commented on August 23, 2024

I seem to remember fixing this for escaped output a long time ago in my original codebase. We can do some rewriting for perf, but it's an easy fix for unescaped output too. I'll push a fix, and we can talk about rewrite and refactor in another GH issue or PR.

from ejs.

mde avatar mde commented on August 23, 2024

Fixed in master in a09d026.

from ejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.