@buxlabs/ejs-engine
v0.3.2
Published
ejs engine
Downloads
13
Readme
ejs-engine
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 & 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?
- lexer converts the source into tokens
- prefixer transforms the identifiers inside of the tokens
- render method concatenates the rendering function based on the tokens
- 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