use vm.Script to avoid reparse #4892
Merged
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.
DISCLAIMER: I don't have enough knowledge in nodejs nor V8. I read most cpp/internal js is by guessing and cramming on cppreference.com :/ I welcome any correction!
This pr tries to alleviate the problem in #4872. When users include third party library into server bundle, the parsing overhead can be smaller.
If user provided global variable is compared with another global variable in library code, they will never be equal because they are executed in different context.
To fix this generally, we have to run library code and user code in the same context. This means we need to parse a lot of code in one request.
We can skip parsing by creating a
Script
object, andrunInNewContext
is effectively a shortcut. Thus it is safe to reuse theScript
object.Reusing
Script
should save several C++ call and reduce garbage collection pressure since the underlying C++ object has aPersistent
which will not be collected until out of memory (it is cargo cult to me, though).However, I cannot see a performance improvement in a simple benchmark, regardless of whether bundling library code into server entry. (I use vue-hackernews as example, tested under
ab -n 1000 -c 100
).It seems v8 will cache compiled code. So parsing overhead is relatively ignorable in real world.
But reusing object is still a good practice, so I submitted this pull request for symbolic value.