Skip to content

Instantly share code, notes, and snippets.

@Dviejopomata
Last active July 27, 2018 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dviejopomata/0040293a7be016f8d931acd3c63018d4 to your computer and use it in GitHub Desktop.
Save Dviejopomata/0040293a7be016f8d931acd3c63018d4 to your computer and use it in GitHub Desktop.
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