Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJs VM - save compiled code... to disk?

Tags:

node.js

v8

From http://nodejs.org/api/vm.html:

JavaScript code can be compiled and run immediately or compiled, saved, and run later. [...] The returned script is not bound to any global object. It is bound before each run, just for that run.

And then in the API, no method that returns any bytes, nothing. Just a "Script" object.

So, before I politely tear down this desk beneath my arms, is there any way that I can actually SAVE the compiled script, to disk? I figure it's just ordinary raw binary data, maybe a syntax tree or whatever.

like image 358
Silviu-Marian Avatar asked Oct 08 '22 12:10

Silviu-Marian


1 Answers

The functions you reference are for javascript being run by javascript in a new context (so it can be secure, have new features, etc)... not so much saving a pre-compiled binary...

If you want details on how to actually reload a precompiled script, you can look at the node.js source. The 'node.js' file itself, is precompiled and loaded as a binary (if you build it with this option). In doing so, it makes node start faster.

What you should bear in mind, however, is there is little advantage to this, unless you conceive a particular process (such as node.js) using the V8 library that will run/stop/run/stop... etc. Reason being, the V8 library will only compile your script once - and will then execute it as machine code each time thereafter, or as long as the V8 library is running.

Precompiling, and loading as a binary, will bring some significant disadvantages, including making your program architecture dependent (even across x86 32-bit versus x86_64) and so forth. So - this may not be the best design decision.

like image 91
EdH Avatar answered Oct 13 '22 10:10

EdH