Subj : Re: Rhino speed To : Dave Thorn From : Igor Bukanov Date : Tue Feb 15 2005 09:39 am Dave Thorn wrote: > Hello, > > I've been investigating rhino as one of the engines to add scripting > to a java application. I've got it running scripts in three ways: > > 1) "Standalone" - Context cx = Context.enter(); (version 1.6R1) > 2) Via Apache's BSF (Bean Scripting Framework) (version 1.5R3) > 3) using jsc - the javascript compiler (version 1.6R1) The compiler was part of Rhino since 1.5R1 release. > > Doing some performance comparisons with native Java code, I found the > rhino scripts were running rather slowly in comparison. Some > operations that took subsecond times in java were taking up to 14 > seconds in any rhino implementation. This was relatively > straightforward code, just a large number of iterations, adding > integers to an ArrayList, and integer comparison. > > The speed difference did surprise me somewhat, but especially so with > the jsc version. I don't know much about bytecode or compilers, but > if jsc compiled it to bytecode, then I was expecting similar speeds to > native Java. JavaScript is dynamically typed language so even operations like x + y goes through special runtime code to check types of x and y and perform operation accordingly. In addition numbers are represented as objects so x + y when x and y are numbers would eventually be evaluated as new Double(((Number)x).doubleValue() + ((Number)y).doubleValue()). In few cases Rhino can optimize that away and generates JVM bytecode similar to what a Java compiler generates for x + y. But as long as x and y comes from object properties or global variables and the result would be stored as a property or in a global variable, the execution would be like the above code. Also all access to Java objects goes through reflection which slows things down. > > Do I simply have naive expectations? Am I perhaps doing something > wrong? Any and all suggestions and comments appreciated. Do not write numerically intense code in JS. And if you need to do that then http://www.mozilla.org/rhino/perf.html may help. Regards, Igor .