@buxlabs/ejs-engine

0.3.2 • Public • Published

ejs-engine

Codeship

Installation

npm install @buxlabs/ejs-engine

Usage

const { compile } = require('@buxlabs/ejs-engine')
const template = compile('<div><%= foo %></div>')
assert(template({ foo: 'bar' }) === '<div>bar</div>')
const escape = require('escape-html')
const { compile } = require('@buxlabs/ejs-engine')
const template = compile('<div><%- foo %></div>')
assert(template({ foo: 'bar & baz' }, escape) === '<div>bar &amp; baz</div>')

Features

  • no with statement inside of the output code,
  • no data prefix for variables is required to speed up the template,
  • can use any escape method you choose,
  • does not allow to use any globals, they must be passed to the template

How does it work?

  1. lexer converts the source into tokens
  2. prefixer transforms the identifiers inside of the tokens
  3. render method concatenates the rendering function based on the tokens
  4. compile method creates a new function instance

How different it is from other ejs templates?

The engine is not intended to be used in the browser directly, the templates should be compiled before being sent to the browser. The engine tokenizes and makes a static analysis of the template to create an optimal rendering method that does not contain any unnecessary code.

Benchmarks

Few tests are in the test/benchmark.js file, please feel free to add more examples.

@buxlabs/ejs-engine x 2,704,910 ops/sec ±1.14% (82 runs sampled)
_.template x 388,450 ops/sec ±1.42% (81 runs sampled)
lodash.template x 473,581 ops/sec ±4.13% (84 runs sampled)
_.template optimized x 2,304,672 ops/sec ±1.70% (79 runs sampled)
lodash.template optimized x 2,599,384 ops/sec ±1.39% (83 runs sampled)
Fastest is @buxlabs/ejs-engine

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i @buxlabs/ejs-engine

Weekly Downloads

1

Version

0.3.2

License

MIT

Unpacked Size

34 kB

Total Files

14

Last publish

Collaborators

  • buxlabs-dev
  • emilos