在node.js中创建具有低延迟的V8实例

时间:2021-04-19 18:59:49

I have a node.js webserver which is supposed to execute a javascript-String which is passed at runtime with low latency. I am basically looking for an node.js version of the following java code:

我有一个node.js webserver,它应该执行一个javascript-String,它在运行时以低延迟传递。我基本上是在寻找以下java代码的node.js版本:

import com.eclipsesource.v8.V8;

public class MainC {

private static V8 v8;   

private static int result;

public static void main(String[] args) {

    long time0 = System.nanoTime();
    createRuntime();       
    long time1 = System.nanoTime();   

    String script = "x(0,10000); "
        + "function x(y,z) { "
        + "     if (z>0) {"
        + "         return x(y+1,z-1); "
        + "     } else {"
        + "         return y;"
        + "     } "
        + "}"
        + ""; 

    simulateHTTPRequestAndExucuteScript(script);
    long time2 = System.nanoTime();          

    System.out.println("Result: " + result);        
    System.out.println("Time for 'createRuntime()' : " + ((time1-time0)/1000000.0) + " ms");
    System.out.println("Time for 'executeIntScript()' : " + ((time2-time1)/1000000.0) + " ms");
}

private static void createRuntime() {
    v8 = V8.createV8Runtime();   
}

private static void simulateHTTPRequestAndExucuteScript(String s) {
    result = v8.executeIntScript(s); 
}

}

which outputs:

Result: 10000
Time for 'createRuntime()' : 741.709313 ms
Time for simulateHTTPRequestAndExucuteScript()' : 0.888719 ms

Runtime creation and execution of the script are two separated tasks. Note, that the actual execution time for 10000 recursive calls is < 1ms. The comparably long time for initializing the runtime is not important, because this can be done before the string is passed.

运行时创建和脚本执行是两个独立的任务。请注意,10000次递归调用的实际执行时间<1ms。初始化运行时的相当长的时间并不重要,因为这可以在传递字符串之前完成。

How can V8 runtime creation and low latency javascript code injection be done with nodes.js?

如何使用nodes.js完成V8运行时创建和低延迟javascript代码注入?

2 个解决方案

#1


0  

Have you tried the vm module of nodejs?

你试过nodejs的vm模块吗?

var vm = require('vm');
var script = "result = x(0,10000); "
    + "function x(y,z) { "
    + "     if (z>0) {"
    + "         return x(y+1,z-1); "
    + "     } else {"
    + "         return y;"
    + "     } "
    + "}"
    + ""; 
var context = {result: null};

vm.createContext(context);

vm.runInContext(script, context);
console.log(context.result);

#2


0  

eh.... node.js IS a v8 runtime wrapper. you don't need to create, inject and so on with node.js read the basic nodejs about page: https://nodejs.org/en/about/ basically you write javascript, run it via node.js (which again, is v8 with extra c++ libs wrapped with js code) and thats it.

呃.... node.js是一个v8运行时包装器。你不需要创建,注入等与node.js一起阅读关于页面的基本nodejs:https://nodejs.org/en/about/基本上你写javascript,通过node.js运行它(再次,是v8与额外的c ++库包裹着js代码)就是这样。

in your case, you main js file that you run via nodejs will do that js code you have (that function (x) ....) and invoke it how many times you want.

在你的情况下,你通过nodejs运行的主js文件将执行你拥有的js代码(该函数(x)....)并调用它你想要多少次。

to run the js file just do "node my-jsfile.js"

运行js文件只需执行“node my-jsfile.js”

thats all.

#1


0  

Have you tried the vm module of nodejs?

你试过nodejs的vm模块吗?

var vm = require('vm');
var script = "result = x(0,10000); "
    + "function x(y,z) { "
    + "     if (z>0) {"
    + "         return x(y+1,z-1); "
    + "     } else {"
    + "         return y;"
    + "     } "
    + "}"
    + ""; 
var context = {result: null};

vm.createContext(context);

vm.runInContext(script, context);
console.log(context.result);

#2


0  

eh.... node.js IS a v8 runtime wrapper. you don't need to create, inject and so on with node.js read the basic nodejs about page: https://nodejs.org/en/about/ basically you write javascript, run it via node.js (which again, is v8 with extra c++ libs wrapped with js code) and thats it.

呃.... node.js是一个v8运行时包装器。你不需要创建,注入等与node.js一起阅读关于页面的基本nodejs:https://nodejs.org/en/about/基本上你写javascript,通过node.js运行它(再次,是v8与额外的c ++库包裹着js代码)就是这样。

in your case, you main js file that you run via nodejs will do that js code you have (that function (x) ....) and invoke it how many times you want.

在你的情况下,你通过nodejs运行的主js文件将执行你拥有的js代码(该函数(x)....)并调用它你想要多少次。

to run the js file just do "node my-jsfile.js"

运行js文件只需执行“node my-jsfile.js”

thats all.