Nashorn can use 'optimistic types' (more below), and they are turned on by default in Java 9 and later, but they cause delays at startup.
Turning off optimistic types yields:
$ time jjs --optimistic-types=false moment-with-locales.js
real 0m4.282s
user 0m0.000s
sys 0m0.015s
The switch can be abbreviated -ot=false
.
jjs -h
defines optimistic types as follows:
Use optimistic type assumptions with deoptimizing recompilation.
This makes the compiler try, for any program symbol whose type cannot
be proven at compile time, to type it as narrow and primitive as
possible. If the runtime encounters an error because symbol type
is too narrow, a wider method will be generated until steady stage
is reached. While this produces as optimal Java Bytecode as possible,
erroneous type guesses will lead to longer warmup. Optimistic typing
is currently enabled by default, but can be disabled for faster
startup performance.
Thus, optimistic typing may yield faster performance in the long term (though that is not guaranteed), but results in slower startup.