diff --git a/benchmark/index.ts b/benchmark/index.ts index c61ad558d..3b48082d4 100644 --- a/benchmark/index.ts +++ b/benchmark/index.ts @@ -1,11 +1,13 @@ import output from './output' import tag from './tag' import demo from './demo' +import layout from './layout' async function main () { await output() await tag() await demo() + await layout() } main() diff --git a/benchmark/layout.ts b/benchmark/layout.ts new file mode 100644 index 000000000..e0d030215 --- /dev/null +++ b/benchmark/layout.ts @@ -0,0 +1,36 @@ +import * as Benchmark from 'benchmark' +import Liquid from '../src/liquid' + +const engineOptions = { + root: __dirname, + extname: '.liquid' +} + +const engine = new Liquid(engineOptions) +const cachingEngine = new Liquid({ + ...engineOptions, + cache: true +}) + +const template = ` +{% layout "./templates/layout.liquid" %} +{% block body %}a small body{% endblock %} +` + +export default function () { + console.log('--- layout ---') + return new Promise(resolve => { + new Benchmark.Suite('layout') + .add('cache=false', { + defer: true, + fn: (d: any) => engine.parseAndRender(template, {}).then(x => d.resolve(x)) + }) + .add('cache=true', { + defer: true, + fn: (d: any) => cachingEngine.parseAndRender(template, {}).then(x => d.resolve(x)) + }) + .on('cycle', (event: any) => console.log(String(event.target))) + .on('complete', resolve) + .run({ 'async': true }) + }) +} diff --git a/benchmark/templates/layout.liquid b/benchmark/templates/layout.liquid new file mode 100644 index 000000000..b6a77babb --- /dev/null +++ b/benchmark/templates/layout.liquid @@ -0,0 +1,2 @@ +This is a barely empty layout with a body: +{% block body %}{% endblock %} diff --git a/src/liquid.ts b/src/liquid.ts index 1dfcfe303..e1f0af588 100644 --- a/src/liquid.ts +++ b/src/liquid.ts @@ -51,9 +51,10 @@ export default class Liquid { const paths = roots.map(root => fs.resolve(root, file, this.options.extname)) for (const filepath of paths) { + if (this.options.cache && this.cache[filepath]) return this.cache[filepath] + if (!(await fs.exists(filepath))) continue - if (this.options.cache && this.cache[filepath]) return this.cache[filepath] const value = this.parse(await fs.readFile(filepath), filepath) if (this.options.cache) this.cache[filepath] = value return value