Perf: Memoize Render plugin's compile functions (#3915)#4229
Open
agentic-sanyam wants to merge 1 commit into11ty:mainfrom
Open
Perf: Memoize Render plugin's compile functions (#3915)#4229agentic-sanyam wants to merge 1 commit into11ty:mainfrom
agentic-sanyam wants to merge 1 commit into11ty:mainfrom
Conversation
Ryuno-Ki
reviewed
Mar 2, 2026
| compileCache.set(templateConfig, new Map()); | ||
| } | ||
| let projectCache = compileCache.get(templateConfig); | ||
| let cacheKey = `${content}###${templateLang || ""}`; |
Contributor
There was a problem hiding this comment.
If you don't touch the assignments, you can use const over let, generally speaking.
Ryuno-Ki
reviewed
Mar 2, 2026
| @@ -1,4 +1,5 @@ | |||
| import test from "ava"; | |||
| import fs from "node:fs"; | |||
Contributor
There was a problem hiding this comment.
You could pull the functions directly during the import:
Suggested change
| import fs from "node:fs"; | |
| import { existsSync, unlinkSync, writeFileSync } from "node:fs"; |
Contributor
|
Note: Account was created two days ago. The activity looks a little bit sus too me. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hey team,
This PR addresses #3915 by adding memoization to the Render plugin's
compileandcompileFilefunctions.The original issue highlighted that repeated calls to
renderFileorcompilewith the same input content or file path could lead to redundant template compilations, especially on larger sites or during watch mode builds. As noted in the issue, implementing similar caching can lead to significant build time improvements, so I figured this would be a great addition to the plugin.Here's a quick rundown of the changes:
compilefunction: Now uses aWeakMap-basedcompileCacheto store and retrieve already compiled template functions. The cache key is generated from the template content and language, meaning if youcompilethe same string multiple times, it only gets processed once.compileFilefunction: Similarly,compileFilenow leverages aWeakMap-basedcompileFileCache. This cache stores the compiled function along with the file's content at the time of compilation. Before returning a cached function, it performs a quick check against the current file content to ensure the cache is invalidated if the file has changed (crucial for watch mode or other file modifications).TemplateConfigobject as aWeakMapkey. This helps ensure proper isolation between different Eleventy instances and allows for efficient garbage collection when a config is no longer needed.These changes should lead to a nice performance boost for sites that heavily use
renderFileorcompile, as it avoids re-compiling the same templates over and over.I've also added a few new tests to
TemplateRenderPluginTest.jsto verify that the caching works as expected, including correctly invalidating the cache when file contents change.Let me know what you think!
Fixes #3915