有什么提示和建议吗?让犀牛表现更快的技巧?

发布于 2024-11-28 09:02:38 字数 109 浏览 4 评论 0原文

有什么提示和建议吗?让犀牛表现更快的技巧?我正在尝试在Rhino中使用uglifyJs压缩一个大的js文件,这需要一分多钟的时间。您对 java 服务器端空间中的 rhino 有任何提示或其他替代方案吗?

Are there any tips & tricks for making rhino perform faster? I'm trying to compress a large js file using uglifyJs in Rhino and it takes more than a minute. Do you have any hints or other alternatives for rhino in java server side space?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒灬来后我 2024-12-05 09:02:38

通过 Rhino 上的 JavaScript API,您可以使用 Compilable 接口简单地编译脚本。例如:

public class CompileScript {
    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager engineManager = new ScriptEngineManager();
        ScriptEngine scriptEngine = engineManager.getEngineByName("js");

        //cast to Compilable engine, this is safe for Rhino
        Compilable c = (Compilable) scriptEngine;    
        CompiledScript script = c.compile("print('Hello World')");    //compile

        script.eval();
    }
}

但是,当多次运行脚本时,这样做的好处就会显现出来。基本上它减少了每次重新解释的开销。来自 CompiledScript javadoc:

由存储编译结果的类扩展。状态可以以 Java 类、Java 类文件或脚本语言操作码的形式存储。该脚本可以重复执行而无需重新解析。

无论如何,我认为你应该看看 Rhino JavaScript 编译器。它“将 JavaScript 源代码转换为 Java 类文件”。

并且有一个 V8 Java 实现。检查 jav8

With the JavaScript API over Rhino you can simply compile the script using the Compilable interface. For example:

public class CompileScript {
    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager engineManager = new ScriptEngineManager();
        ScriptEngine scriptEngine = engineManager.getEngineByName("js");

        //cast to Compilable engine, this is safe for Rhino
        Compilable c = (Compilable) scriptEngine;    
        CompiledScript script = c.compile("print('Hello World')");    //compile

        script.eval();
    }
}

However the benefits of this will show up when running several times the script. Basically it reduces the overhead of re-interpret every time. From the CompiledScript javadoc:

Extended by classes that store results of compilations. State might be stored in the form of Java classes, Java class files or scripting language opcodes. The script may be executed repeatedly without reparsing.

Anyway I think you should take a look at the Rhino JavaScript Compiler. It "translates JavaScript source into Java class files".

And there is a V8 Java implementation. Check jav8.

暗藏城府 2024-12-05 09:02:38

v8

或者尝试使用“编译”模式而不是“解释”模式。

v8

or try to use "compiling" mode instead of "interpreting" mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文