Today I learned: Node’s `vm.createContext` function is quite slow!
https://nodejs.org/api/vm.html#vmcreatecontextcontextobject-options
Some big (dare I say huge) WebC performance wins incoming 🏆
6 replies
@zachleat Doesn't it need to bootstrap a whole new Realm with its own copy of the intrinsics?
$ node
Welcome to Node.js v19.6.0.
Type ".help" for more information.
> let otherArray = vm.runInContext('Array', vm.createContext())
undefined
> Array === otherArray
false
>@zachleat If you need a way to create a light-weight scope to prevent unintentional bleed of "global" assignments, you can do tricks via
withover a Proxy over globals.globalThis.x = 'global.x';
let localGlobal = Object.create(globalThis);
let p = new Proxy(
localGlobal,
{
has(obj, prop) {
return true;
},
get(obj, prop) {
if (prop === 'globalThis') { return localGlobal }
return obj[prop];
},
}
);
with (p) {
console.log('x before assignment', x); // => global.x
x = 'local-this';
console.log('x after assignment', x); // => local-this
console.log('globalThis.x', globalThis.x); // => local-this
}
console.log('x outside `with`', x); // => global.xDoesn't provide mutual suspicion and doesn't solve functions that return the global scope but keeps a lid on unintentional interference.
@mvsamuel yeah! I did something similar, though with a scoped object that I could blow away for a cheap clean slate
@mvsamuel Yeah, I believe so!
-
@zachleat get out of town! 👏🏼👏🏼👏🏼👏🏼👏🏼👏🏼