Repository navigation
Conversation
unwrapReturns split the whole source into an array of characters and
joined it back on every call, even when no return statement outside a
function had to be replaced. For large sources that copy costs more than
everything else addWith does after parsing: on a 389 KB template body
generated by Pug it took about 7 ms of 29 ms.
Create the array on the first replacement instead and return the source
as is when there was none. The output does not change: joining an
untouched split('') gives the original string back.
|
This PR will not result in a new version of with as there are no user facing changes. |
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.
unwrapReturnssplits the whole source into an array of characters and joins it back on every call, even when there is noreturnoutside a function to replace. For large sources this copy is a noticeable share ofaddWith. Pug callsaddWithon the entire generated code of a template, which never has a top-levelreturn, so every Pug compile pays for it.This PR creates the array lazily, on the first replacement, and returns the source as is when nothing was replaced. When there is a
returnto unwrap, the code path is exactly the same as before.Output is unchanged.
src.split('').join('')issrc, so skipping both gives the same string. To check it in practice I ran the released 7.0.2 and this branch side by side and compared the returned strings (and the thrown errors) byte for byte:excludelists and object arguments, plus the same bodies withreturnstatements added at the start, middle and end;.jsfiles from anode_modulestree, as is and with a top-levelreturnappended;returnat top level, insideif, loops,switch,try, and inside nested functions, arrow functions, class and object methods, with non-ASCII and astral characters around the replaced ranges;38,256 calls in total, with both
@babel/parser7.9.6 (the lockfile version) and 7.29.7: no differences. Existing tests, lint and prettier pass.Timing.
addWith('locals || {}', src, exclude)on code generated by Pug; median of 30 runs per version after 10 warm-up runs, versions alternating; Node 22.21.1,@babel/parser7.29.7, Apple M4:returnNo public API, dependency or parser changes.