Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2011 18:25
Show Gist options
  • Save axt/1240928 to your computer and use it in GitHub Desktop.
Save axt/1240928 to your computer and use it in GitHub Desktop.
Runninc sunspider benchmarks to compare Rhino and V8 performance
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import lu.flier.script.V8Array;
public class V8Test {
public static void main(String[] args) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine v8xx = factory.getEngineByName("jav8");
ScriptEngine v8engine = factory.getEngineByName("jav8");
ScriptEngine rhinoengine = factory.getEngineByName("rhino");
System.err.println("V8 :" + v8engine);
System.err.println("Rhino:" + rhinoengine);
String input1 = FileUtils.readFileToString(new File("sunspider-test-prefix.js"));
String input2 = FileUtils.readFileToString(new File("sunspider-test-contents.js"));
V8Array tests = (V8Array)v8xx.eval("tests");
V8Array testContents = (V8Array)v8xx.eval("testContents");
List<String> scriptSources = new ArrayList<String>();
for(int i=0; i<tests.size();i++) {
String testName = (String)tests.get(i);
String testScript = (String)testContents.get(i);
Pattern p = Pattern.compile("var _sunSpiderStartDate = new Date\\(\\);(.*?)var _sunSpiderInterval = new Date\\(\\) - _sunSpiderStartDate;",Pattern.DOTALL | Pattern.MULTILINE);
Matcher m = p.matcher(testScript);
testScript =;
int c = 0;
for(String scriptSource : scriptSources) {
Compilable compilable = (Compilable) rhinoengine;
CompiledScript script = compilable.compile(scriptSource);
System.err.println("Running script nr " + tests.get(c++));
long before, after;
Object res;
before = System.nanoTime();
res = v8engine.eval(scriptSource);
after = System.nanoTime();
System.err.println("V8 : " +((after-before)/1000/1000)+" ms");
before = System.nanoTime();
res = script.eval();
after = System.nanoTime();
System.err.println("Rhino: " +((after-before)/1000/1000)+" ms");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment