-
-
Save bryanhelmig/c7bed4576c7e270796a4 to your computer and use it in GitHub Desktop.
record time spent on "each" run for nodejs or node vm module vs contextify, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vm = require('vm'), | |
Contextify = require('contextify'), | |
code = 'var square = n * n;', | |
fn = new Function('n', code), | |
script = vm.createScript(code); | |
n = 5; | |
benchmark = function(title, funk) { | |
var end, i, start, spins = 10000; | |
start = new Date; | |
for (i = 0; i < spins; i++) { | |
funk(); | |
} | |
end = new Date; | |
console.log(title + ': ' + (end - start) / spins + 'ms per run'); | |
} | |
var ctx = vm.createContext({n: n}); | |
var reusedSandbox = {n: n}; | |
Contextify(reusedSandbox); | |
benchmark('vm.runInThisContext', function() { vm.runInThisContext(code); }); | |
benchmark('vm.runInNewContext', function() { vm.runInNewContext(code, {n: n}); }); | |
benchmark('script.runInThisContext', function() { script.runInThisContext(); }); | |
benchmark('script.runInNewContext', function() { script.runInNewContext({n: n}); }); | |
benchmark('script.runInContext', function() { script.runInContext(ctx); }); | |
benchmark('reused contextify', function() { reusedSandbox.run(code); }); | |
benchmark('new contextify', function() { var sandbox = {n: n}; Contextify(sandbox); sandbox.run(code); sandbox.dispose(); }); | |
benchmark('eval', function() { eval(code); }); | |
benchmark('fn', function() { fn(n); }); | |
/* | |
vm.runInThisContext: 0.0011ms per run | |
vm.runInNewContext: 0.2967ms per run | |
script.runInThisContext: 0.0008ms per run | |
script.runInNewContext: 0.2881ms per run | |
script.runInContext: 0.1716ms per run | |
reused contextify: 0.0018ms per run | |
new contextify: 0.3195ms per run | |
eval: 0.001ms per run | |
fn: 0ms per run | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For iojs - its a bit slower:
Nodejs proper is a bit faster:
Both in boot2docker containers.