Javet
Javet copied to clipboard
Preload script for later use
Is there a way to pre-load a script for later use?
Imagine that I have script A and script B, script A never changes but script B is dynamic, is there a way to preload script A inside an executor and execute script B in the same executor that script A is preloaded?
I think ESM might be the one you are looking for.
Javet allows the applications to load ES modules via import {...} from 'module'
. Those modules can be actively cached by V8 in a lazy-loading manner, or if you want to, you may have the applications preload them.
Please refer to this doc for detail.
Thanks @caoccao , the ESM would work but unfortunately the script is not ES Module compatible.
Are values assigned to the GlobalObject immediately copied and cached inside the runtime?
this.runtime.getGlobalObject().set("staticScript", script);
You cannot set a script as a string literal directly, hoping it works. Because it remains a string in V8. You have to set a function which has already been compiled from a script string.
If you set a function to any objects including the global object, yes, it becomes effective immediately.
I suggest you to inject a proxied Java object if that's possible with the following benefits.
- No compilation
- No additional memory
- Super fast