我正在编写一个节点服务器,使用express和ejs作为诱人引擎。我的一些.ejs文件有7 - 8个包含嵌套在其中的局部的语句。我想知道这是否是资源密集型,或者它是否会好起来。
答案 0 :(得分:4)
EJS维护者在这里。
首先,如果您连续多次读取同一文件,文件系统会将其缓存在该级别上。
但是,如果您想获得最佳性能,请在选项中设置cache: true
。 (docs)您必须使用renderFile()
或传入文件名才能使其正常工作。如果您正在使用Express,则cache: true
设置为NODE_ENV
时会自动设置production
。
请记住,这假设您的文件永远不会更改。即如果您更改文件,EJS将只使用旧的缓存版本,直到您重新启动服务器进程。
答案 1 :(得分:1)
我已经测试了29个部分的包含加载时间:
<!DOCTYPE html>
<html lang="en">
<? console.time('ejsIncludeTime') ?>
<?include ../partials/head ?>
<body>
<? include ../partials/header ?>
<? include ../partials/a ?>
<? include ../partials/b ?>
<? include ../partials/c ?>
<? include ../partials/d ?>
<? include ../partials/e ?>
<? include ../partials/f ?>
<? include ../partials/g ?>
<? include ../partials/h ?>
<? include ../partials/i ?>
<? include ../partials/j ?>
<? include ../partials/k ?>
<? include ../partials/l ?>
<? include ../partials/m ?>
<? include ../partials/n ?>
<? include ../partials/o ?>
<? include ../partials/p ?>
<? include ../partials/q ?>
<? include ../partials/r ?>
<? include ../partials/s ?>
<? include ../partials/t ?>
<? include ../partials/u ?>
<? include ../partials/v ?>
<? include ../partials/w ?>
<? include ../partials/x ?>
<? include ../partials/y ?>
<? include ../partials/z ?>
<? include ../partials/footer ?>
<? console.timeEnd('ejsIncludeTime') ?>
</body>
</html>
a-z的每个部分包含120行或更多行的html。这是怎么回事:
ejsIncludeTime: 0.175ms
ejsIncludeTime
:0.078ms
,0.068ms
,0.058ms
,0.067ms
,0.077ms
ejsIncludeTime: 0.157ms
0.168ms
(在几分钟差距后点击),0.044ms
,0.052ms
因此看起来在某种程度上涉及到缓存。第一次命中总是需要0.150 + ms的包含时间。随之而来的命中总是耗费更少的时间,除了偶尔的峰值,如0.168ms(无法解释原因。也许缓存未命中)。