Giter Club home page Giter Club logo

Comments (8)

oubidar-Abderrahim avatar oubidar-Abderrahim commented on September 23, 2024

Hi, Thank you for reaching out about this, Could you please share the full example with steps to reproduce using GraalJS and other engines you've tested like V8, and we will take a look into it.

from graaljs.

omarmahamid avatar omarmahamid commented on September 23, 2024

@oubidar-Abderrahim

the backend is something like

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);
        }


    }
}````


the subjectJson is a json file with 50K row


the js is expression resolver 


something like (not exactly the js code, but some idea)




// Create a utility object for handling expressions
var ExpressionProcessor = {
    // Compile an expression
    compile: function (exp) {
        var originalExp = exp;
        var dateMatch = exp.match(dateRegex);
        var value;
        var format;

        if (dateMatch) {
            value = dateMatch[1];
            format = null;

            if (dateMatch.length > 2 && dateMatch[2]) {
                format = "'" + dateMatch[2] + "'";
            }

            exp = "formatDate(" + value + "," + format + ")";
        }

        var amountMatch = exp.match(amountRegex);
        if (amountMatch) {
            value = amountMatch[1];
            format = null;
            var currency = null;

            var amountMatch2 = amountMatch[2];
            if (typeof amountMatch2 !== "undefined" && amountMatch2 !== null && amountMatch2.length > 0) {
                format = "'" + amountMatch[2] + "'";
            }

            if (typeof amountMatch[3] !== "undefined" && amountMatch[3] !== null) {
                currency = amountMatch[3];
            }

            if (typeof currency == "string") {
                currency = currency.replace(/^\s+|\s+$/g, "");
                if (currency === "") {
                    currency = '""';
                }
            }

            exp = "formatAmount(" + value + ", " + currency + ", " + format + ")";
            exp = originalExp.replace(amountRegex, function () {
                return exp;
            });
        }

        var emojiMatch = exp.match(emojiRegex);
        if (emojiMatch) {
            exp = "'{{" + exp + "}}'";
        }

        var stringMatch = exp.match(stringRegex);
        var maxLength = null;
        if (stringMatch) {
            value = stringMatch[1];
            var stringMatch2 = stringMatch[2];
            if (typeof stringMatch2 !== "undefined" && stringMatch2 !== null && stringMatch2.length > 0) {
                maxLength = stringMatch[2];
            }

            exp = "limitString(" + value + "," + maxLength + ")";
            exp = originalExp.replace(stringRegex, exp);
        }
        return exp;
    },

    // Evaluate an expression
    eval: function (exp, ctx, parentCtx, options) {
        var hasError = false;
        var errorObj = null;
        try {
            console.log("Evaluating expression: '" + exp + "'");
            console.log(exp);
            var d = new Date();
            var result = this.evalNode(exp, ctx, parentCtx, options);
            var t = new Date();
            console.log("Expression evaluation time (ms): " + (t - d));
            if (typeof result === "undefined" || result === null) {
                hasError = true;
                errorObj = new Error("Invalid result after evaluating expression: '" + result + "'");
            }
        } catch (e) {
            hasError = true;
            errorObj = { message: "Error - Could not evaluate expression Error Message: " + e.message, stack: e.stack };
        }

        if (hasError) {
            console.error(errorObj.message);
        } else {
            return result;
        }
    },

    // Evaluate a node within an expression
    evalNode: function (node, ctx, parentCtx, options) {
        // Implement node evaluation logic here
    },

    // Get a property from an object
    getChild: function (obj, prop, ctx, parentCtx, options) {
        // Implement property retrieval logic here
    },

    // Check if an object is a valid node
    isNode: function (node) {
        // Implement node type checking logic here
    }
};

// Regular expressions for matching different expression patterns
var dateRegex = /* ... */;
var amountRegex = /* ... */;
var emojiRegex = /* ... */;
var stringRegex = /* ... */;



from graaljs.

omarmahamid avatar omarmahamid commented on September 23, 2024

@oubidar-Abderrahim

anyway,

the issue is that with V8 wrapper to JAVA, it works much much faster.

but with graaljs, it works much much slower

my question is, should I use specific compiler for graaljs ? to optimize the process ? like JIT compiler ?

how ? any example ?

from graaljs.

oubidar-Abderrahim avatar oubidar-Abderrahim commented on September 23, 2024

Hi @woess, can you take a look into this please?

from graaljs.

oubidar-Abderrahim avatar oubidar-Abderrahim commented on September 23, 2024

@omarmahamid could you please verify this issue with the newest build of graalJS, also please specify the version you were using before. Thank you

from graaljs.

omarmahamid avatar omarmahamid commented on September 23, 2024

@oubidar-Abderrahim

can you please explain what changed since 23.0.0 and the new version 23.1.1 ?
which features the new version contains ?

from graaljs.

oubidar-Abderrahim avatar oubidar-Abderrahim commented on September 23, 2024

You can check the release notes for 23.0 here and for 23.1 here

from graaljs.

woess avatar woess commented on September 23, 2024

I can't comment on this without a runnable reproducer that demonstrates the performance problem.

So my only advice would be to ensure that compiled code can be cached between runs, e.g. in case of multiple contexts evaluating the same sources, create the Contexts with a shared Engine, evaling shared Sources; and in case of one-off runs, consider use of Native Image.

from graaljs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.