Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rhino over Duktape #82

Closed
HugoGresse opened this issue Dec 9, 2016 · 7 comments
Closed

Rhino over Duktape #82

HugoGresse opened this issue Dec 9, 2016 · 7 comments

Comments

@HugoGresse
Copy link

Hi,

I was searching for a JS engine for Android and it seems that Rhino is ~10% more efficient than Duktape. What do you think about it? Maybe a real benchmark could be helpful...

@swankjesse
Copy link
Contributor

Please do! Benchmarking on Android would be particularly useful since Rhino relies on the host VM whereas Duktape doesn’t.

I doubt we’d switch regardless of the numbers because we’re optimizing for compactness and not throughput.

@HugoGresse
Copy link
Author

HugoGresse commented Dec 9, 2016

for what I see, runing 1000x the same operation which is creating a js function that return 1 and calling it and counting the return value in java, so the whole java > js > java chain is 10% faster using Rhino. What's more, Rhino is 600ko once packaged, Duktape is 2mB. To finish, Rhino is java based, no JNI which reduce the crash potential drastically.

DUKTAPE


    public static final String COMMAND ="function runTest() { return 1; }\n" +
            "var result = runTest();\n";
    Duktape mDuktape;

    public DuktapeTest(Listener listener, int loopNumber, Context context) {
        super(listener, loopNumber, context);

        mDuktape = Duktape.create();

        mDuktape.evaluate(COMMAND);


        long before = System.nanoTime();
        int result = 0;

        try {
            for(int i = 0; i < mLoopNumber; i++){
                result += test();
            }
        } finally {
            mDuktape.close();
        }

        if(result != mLoopNumber) {
            Log.w("Duktape", "Error loop end not reached correctly");
            super.onResult(-1);
            return;
        }

        super.onResult(System.nanoTime() - before);
    }

    private double test(){
        return Double.parseDouble(String.valueOf(mDuktape.evaluate("runTest()")));
    }

RHINO

    org.mozilla.javascript.Context mJSContext;

    public RhinoJarTest(Listener listener, int loopNumber, Context context) {
        super(listener, loopNumber, context);

        mJSContext = org.mozilla.javascript.Context.enter();

        mJSContext.setOptimizationLevel(-1);

        Scriptable scope = mJSContext.initStandardObjects();

         mJSContext.evaluateString(scope, "function runTest() { return 1; }", "ScriptAPI", 1, null);

        long before = System.nanoTime();
        int result = 0;

        try {
            for(int i = 0; i < mLoopNumber; i++){
                result += test(scope);
            }
        } finally {
            org.mozilla.javascript.Context.exit();
        }

        if(result != mLoopNumber) {
            Log.w("Rhino", "Error loop end not reached correctly");
            super.onResult(-1);
            return;
        }

        super.onResult(System.nanoTime() - before);
    }

    private double test(Scriptable scope){
        return Double.parseDouble(String.valueOf(mJSContext.evaluateString(scope, "runTest()", "ScriptAPI", 1, null)));
    }

@swankjesse
Copy link
Contributor

Neat! Which OS / device? Mind providing the specific numbers? Also what's the time elapsed to run a single operation?

@swankjesse
Copy link
Contributor

(I’m concerned about the potential time to initialize the JS engine. The 10% faster thing means less if it's 20% slower to initialize!)

@HugoGresse
Copy link
Author

I'll probably publish a repo with the benchmarks (how to execute JS in Android). Anyways, here is a result. I should probably run some real average on the result and complete the JS code with real calculation (not just returning values). Anyways, the AndroidJSCore is pretty bad.

@HugoGresse
Copy link
Author

HugoGresse commented Dec 9, 2016

It didn't count the initialize time, but based on my eye duktape and Rhino initialize time are pretty correct compared to webview for example.

Moto X 2014 android 5.1

@swankjesse
Copy link
Contributor

Cool. Looking forward to seeing your repo with benchmarks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants