mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
perf: improve getTemplate() when cache is enabled
Checking for file existence is unnecessary when cache contains entry.
This commit is contained in:
committed by
Jun Yang
parent
bbebb5515c
commit
1ffba2bc76
@@ -1,11 +1,13 @@
|
|||||||
import output from './output'
|
import output from './output'
|
||||||
import tag from './tag'
|
import tag from './tag'
|
||||||
import demo from './demo'
|
import demo from './demo'
|
||||||
|
import layout from './layout'
|
||||||
|
|
||||||
async function main () {
|
async function main () {
|
||||||
await output()
|
await output()
|
||||||
await tag()
|
await tag()
|
||||||
await demo()
|
await demo()
|
||||||
|
await layout()
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -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 })
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
This is a barely empty layout with a body:
|
||||||
|
{% block body %}{% endblock %}
|
||||||
+2
-1
@@ -51,9 +51,10 @@ export default class Liquid {
|
|||||||
const paths = roots.map(root => fs.resolve(root, file, this.options.extname))
|
const paths = roots.map(root => fs.resolve(root, file, this.options.extname))
|
||||||
|
|
||||||
for (const filepath of paths) {
|
for (const filepath of paths) {
|
||||||
|
if (this.options.cache && this.cache[filepath]) return this.cache[filepath]
|
||||||
|
|
||||||
if (!(await fs.exists(filepath))) continue
|
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)
|
const value = this.parse(await fs.readFile(filepath), filepath)
|
||||||
if (this.options.cache) this.cache[filepath] = value
|
if (this.options.cache) this.cache[filepath] = value
|
||||||
return value
|
return value
|
||||||
|
|||||||
Reference in New Issue
Block a user