Comments (8)
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.
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.
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.
Hi @woess, can you take a look into this please?
from graaljs.
@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.
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.
You can check the release notes for 23.0 here and for 23.1 here
from graaljs.
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 Context
s with a shared Engine
, eval
ing shared Source
s; and in case of one-off runs, consider use of Native Image.
from graaljs.
Related Issues (20)
- [23.1.0] WarnInterpreterOnly=false is ignored HOT 3
- [23.1.0] Unable to find truffle-api/polyglott in embedded assembly HOT 5
- Maps are not processed with JSON.stringify HOT 3
- [23.1.0] Error creating a GraalJS Engine in a Spring Boot app HOT 4
- Unable to use JavaScript Array in Java method requiring a Collection HOT 1
- How to install a Graalvm for Jdk 21 with included java, javascript and node.js on Linux (Ubuntu) HOT 2
- Do the maven artifacts for graaljs 23.0.0 onwards require Java 17 HOT 3
- GraalVM NodeJS AsyncLocalStorage context gets lost after await HOT 1
- Webpack example configs? HOT 1
- Number.isInteger is incorrectly false for a java.lang.Long HOT 3
- Pass JPA Entity Object as parameter to JavaScript Function HOT 7
- About Compiling the 'GraalJS' Project into an Executable File
- Performance degradation when moving to GraalJS 23.1.1 (and GraalVM for JDK21) HOT 1
- How to use Graal JavaScript engine on a stock JDK HOT 1
- How to do error handling on custom truffle filesystems HOT 3
- Numeric wrapper classes coverted based on its values HOT 2
- Method Signature Mismatch When Calling Java Method from JavaScript in GraalVM HOT 2
- TypeScript support HOT 2
- No Graaljs and Graalvm node.js native community jvm images for 23.1.1 for windows HOT 2
- Instanceof check fails for custom error subtype using prototype setup HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graaljs.