Last active
July 27, 2018 15:49
-
-
Save Dviejopomata/0040293a7be016f8d931acd3c63018d4 to your computer and use it in GitHub Desktop.
This file contains 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
const vm = require("vm") | |
const Module = require("module") | |
const array = [1, 2, 3, 4, 5, 6] | |
const exp = "n => n === 5" | |
const script = new vm.Script(` | |
module.exports = (array, exp) => { | |
return array.filter(eval(\`\${exp}\`)) | |
} | |
`) | |
const sandbox = Object.assign( | |
{ | |
module: new Module("f1.js", null), | |
}, | |
global, | |
) | |
console.time("vm-load") | |
script.runInNewContext(sandbox, { timeout: 1000 }) | |
console.timeEnd("vm-load") | |
console.time("vm") | |
sandbox.module.exports(array, exp) | |
console.timeEnd("vm") | |
console.time("eval") | |
array.filter(eval(`${exp}`)) | |
console.timeEnd("eval") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment