Take the 2-minute tour ×

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.

share|improve this question
Why would you need to save it to disk? What do you intend on doing with it? You do realize that all javascript is interpreted into memory at load, and then the engine really doesn't ever re-read the file for instruction processing. The compilation is the read. –  jcolebrand Jun 16 '12 at 5:18
I was actually planning to sell the binary result as a close-source nodejs app. –  GRIGORE-TURBODISEL Jun 16 '12 at 5:40
That somebody with your level of experience on the network doesn't understand the concept of "put more information into the problem so that people can give you a right answer". –  jcolebrand Jun 16 '12 at 5:55
Got the girls, did shave the beard, donated 9 inches to Pantene Beautiful Lengths. Knowing that you want it for a for-sale binary-only product tells me what you want to do with the file you're outputting. Like how you would prefer it be obfuscated, if possible, etc. Somebody is likely already releasing a package for node that helps to compile stuff just like you're describing you need. I wouldn't infer that anyone was going to use any code for any particular use, aside from what they put in the problem. –  jcolebrand Jun 16 '12 at 6:14
This link may give you more ideas for what you want (such as providing a C++ add-in that can be better optimized and obfuscated ... etc) groups.google.com/forum/#!topic/nodejs/mPIcq5mHihM –  jcolebrand Jun 16 '12 at 6:15
show 3 more comments

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
I, like you, certainly hope this is what OP was asking. Now I'm sad panda I didn't type this answer first –  jcolebrand Jun 16 '12 at 5:40
add comment

Your Answer


