Keeping ScriptEngine per Thread?

Frantzius, Jörg Joerg.Frantzius at
Wed Dec 9 09:28:13 UTC 2015


we’re using Oracle JDK’s Nashorn in a web application, where it is used to render HTML markup in HTTP worker threads.

I remember having read somewhere that the creation of a ScriptEngine (i.e. invocation of ScriptEngineFactory.getScriptEngine()) may have considerable performance overhead, so probably it should be avoided to do this with every incoming HTTP request. This is why we’ll go for keeping a ScriptEngine per thread in a ThreadLocal.

On the other hand this may result in a large memory footprint, as there can be 200 HTTP worker threads, but we don’t have any experience with it yet.

The ScriptEngineFactory.getParameter() Javadocs<> does list some interesting parameters for the key „THREADING“, that seem to make it possible to have a singleton ScriptEngine instead, which could be used concurrently from multiple threads. However, I have no clue whether this is either supported by Nashorn, nor how to set this parameter on engine creation.

Is there per chance some recommended usage pattern for ScriptEngine in a web server?

Thanks for any insights,


Dipl. Inf. Jörg von Frantzius, Technical Director

E-Mail joerg.frantzius at

Phone +49 30 283921-0
Fax +49 30 283921-29

Aperto AG - In der Pianofabrik
Chausseestraße 5, D-10115 Berlin<>

HRB 77049 B, AG Berlin Charlottenburg
Vorstand: Dirk Buddensiek (Vorsitzender), Kai Großmann, Stephan Haagen, Daniel Simon
Aufsichtsrat: Bernd Hardes (Vorsitzender)

More information about the nashorn-dev mailing list