It there any way to convert javascript source into some pre-compiled stated that can be stored and loaded somehow to org.graalvm.polyglot.Context
instead of eval-ing it as a raw String? Something like undocumented --persistent-code-cache
in nashorn.
Run Java FasterThe compiler of GraalVM provides performance advantages for highly abstracted programs due to its ability to remove costly object allocations in many scenarios.
The GraalVM Polyglot API enables your applications running in GraalVM to execute code in multiple other languages from within your application. For instance, from a Java application you can execute JavaScript code. If you use GraalVM as your Java SDK - then you already have the Polyglot API available on the classpath.
Twitter are one company using GraalVM in production today, and they say that for them it is paying off in terms of real money saved. Twitter are using GraalVM to run Scala applications — GraalVM works at the level of JVM bytecode so it works for any JVM language.
As of May'19, you can share code within the same process to avoid reparsing (similar to the Nashorn code persistence) by reusing the same Engine
object between different Context
s like this:
try (Engine engine = Engine.create()) {
Source source = Source.create("js", "21 + 21");
try (Context context = Context.newBuilder().engine(engine).build()) {
int v = context.eval(source).asInt();
assert v == 42;
}
try (Context context = Context.newBuilder().engine(engine).build()) {
int v = context.eval(source).asInt();
assert v == 42;
}
}
More details can be found here: https://www.graalvm.org/docs/graalvm-as-a-platform/embed/#enable-source-caching
We have plans to support persistent code cache across processes in combination with the GraalVM native-image tool in the future. We already support creating native-images that contain the JavaScript interpreter and the GraalVM compiler. We want to add support for allowing to include pre-warmed up scripts, hopefully with pre-compiled JavaScript native-code as well. So you might be able to start your JS application with close to zero startup time. No ETA though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With