Subj : Re: Performance of calling external Java method as compared to native To : Neil Kolban From : Igor Bukanov Date : Mon Oct 06 2003 09:48 am Neil Kolban wrote: > Folks, > I am looking at embedding the Rhino JavaScript engine in a product. To that > end, I have been examining performance capabilities. > > I have a Java class called "A" that has a method called "callMe" that takes > a string as a value and does nothing with it. > > In JavaScript, I have the following: > > var i; > myA = new A(); > for (i=0; i<2000000; i++) > { > myA.callMe("Hello"); > } Try to place that code inside a function and use var to declare myA like: function doit() { var i; var myA = new A(); for (i=0; i<2000000; i++) { myA.callMe("Hello"); } } doit(); since that would allow for Rhino to optimize access to i and myA. In top-level scripts i++ requires to perform at least new Double(((Number)object_representing)i).doubleValue()) which is not that fast. > > > In Java, I have the following > > A myA = new A(); > for (i=0; i<2000000; i++) > { > myA.callMe("Hello"); > } > > In JavaScript, the code takes 3578 milliseconds to execute while on the same > machine, the Java code takes 484 milliseconds. Is this expected? Is there > any way I can improve the time to call a Java function? Rhino CVS tip contains some performance improvements that may help you but in general if this is indeed the issue for your application, you can implement org.mozilla.javascript.Function for your functions so your code would not need to go through Java reflection which requires to create many additional wrapper objects. Regards, Igor .