Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var vm = require('vm'),
- code = 'var square = n * n;',
- fn = new Function('n', code),
- script = vm.createScript(code),
- sandbox;
- n = 5;
- sandbox = { n: n };
- benchmark = function(title, funk) {
- var end, i, start;
- start = new Date;
- for (i = 0; i < 5000; i++) {
- funk();
- }
- end = new Date;
- console.log(title + ': ' + (end - start) + 'ms');
- }
- benchmark('vm.runInThisContext', function() { vm.runInThisContext(code); });
- benchmark('vm.runInNewContext', function() { vm.runInNewContext(code, sandbox); });
- benchmark('script.runInThisContext', function() { script.runInThisContext(); });
- benchmark('script.runInNewContext', function() { script.runInNewContext(sandbox); });
- benchmark('fn', function() { fn(n); });
- /*
- vm.runInThisContext: 12ms
- vm.runInNewContext: 2210ms
- script.runInThisContext: 7ms
- script.runInNewContext: 2199ms
- fn: 0ms
- */
Add Comment
Please, Sign In to add comment