How many statements that hinder performance include?
I am currently writing a node server using express and ejs for an enticing engine. Some of my .ejs files have 7-8 inclusions for nested snippets. I was wondering if this is resource intensive or if everything will be fine.
EJS.
First, if you read the same file many times in a row, the file system will cache it at that level.
However, if you want the best performance, set cache: true
in the options. ( docs ) You have to use renderFile()
or pass the filename to get this to work. If you are using Express, it is cache: true
installed automatically if the parameter is NODE_ENV
set to production
.
Keep in mind that this assumes that your files never change. that is, if you change the file, EJS will only use the old cached version until you restart the server process.
I tested load times for 29 particles:
<!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>
Each az part contains 120 or more lines of html. Here's how it's done:
- First click after server restart
ejsIncludeTime: 0.175ms
- Subsequent images
ejsIncludeTime
:0.078ms
,0.068ms
,0.058ms
,0.067ms
,0.077ms
- Reboot the server again and click
ejsIncludeTime: 0.157ms
- Subsequent hits
0.168ms
(blows after a few minutes' space)0.044ms
,0.052ms
So it looks like caching is involved at some level. The first hit always takes 0.150 + ms on time. Subsequent hits are always much less time-consuming except for an occasional high such as 0.168ms (really can't explain why. Maybe miss the cache).