Skip to content

Performance issue - A lot of time for invoke js method #756

Open
@omarmahamid

Description

@omarmahamid

Hello All,

Describe the issue

I've encountered a performance problem while using the GraalVM Polyglot API to run JavaScript scripts as a script engine. The specific section of code that invokes the JavaScript function seems to have a significant delay, taking approximately 400 ms on average when executed 6,000 times.

Interestingly, when I attempted to run the same JavaScript code using Node.js in the WebStorm IDE, it only took about 20 ms. This stark difference in rendering time has me wondering if such a substantial delay is to be expected when using GraalVM Polyglot.

I noticed that other script engines, such as V8 and J2V8, tend to complete this task in approximately 50 ms, which further highlights the performance gap I'm encountering with GraalVM.

Below is the code snippet I'm using:

Code snippet or code repository that reproduces the issue


import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;

public class GraalVM{
public static void main(String[] args) {

        try (Context context = Context.create()) {

            URL resourceURL = GraalVM.class.getClassLoader().getResource("script.js");

            URL resourceJson = GraalVM.class.getClassLoader().getResource("story.json");

            context.eval("js", Resources.toString(resourceURL, Charset.defaultCharset()));

            Value functionsObject = context.getBindings("js");


            Value processor = functionsObject.getMember("scripting");

            String subjectJson = Resources.toString(resourceJson, Charset.defaultCharset());

            long start = System.currentTimeMillis();

            System.out.println(processor.execute(subjectJson));

            System.out.println("time took is " + (System.currentTimeMillis() - start) + " ms");


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
} ````


Expected behavior

I'm hoping to optimize the execution time by utilizing the GraalVM Polyglot API more effectively. To provide some context, the story.json file contains around 50,000 rows of data, and the scripting method involves the usage of jsep for parsing and some Visitor Design Pattern, which includes recursion.

Some Input Context

the story.json is a json of about 50K rows with data
the method scripting, is using

```` var jsep = require("jsep"); ```` 

and some Vistor Design Pattern .. that go through it.

and it contains some recursion

val = callee.call(args);

any ideas ??

Metadata

Metadata

Labels

performancePerformance of the engine (peak or warmup)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions