Now this is possible because I added support for the timeout parameter to the Node vm module. You can simply pass the timeout value in milliseconds to runInNewContext() , and it will throw an exception if the code does not complete execution within the specified period of time.
Please note: this does not imply any security model for running untrusted code. It just allows you a timeout that you trust or otherwise protect.
var vm = require("vm"); try { vm.runInNewContext("while(true) {}", {}, "loop", 1000); } catch (e) {
Exactly what you expect:
$ time ./node test.js finished real 0m1.069s user 0m1.047s sys 0m0.017s
source share