Giter Site home page Giter Site logo

Comments (9)

XmiliaH avatar XmiliaH commented on June 12, 2024 2

VM2 does not allow for es modules, so there is no dynamic import.

from vm2.

XmiliaH avatar XmiliaH commented on June 12, 2024 1

There is currently no way to improve this as require files are not cached. You can try the following hack which gave me a 10% speed boost.

const options = {
	require: { external: true, root: './'}
};
const script = new VMScript(`module.exports = require('uuid');`);
const { packageCache, scriptCache } = new NodeVM(options)._resolver ?? {};

function render(args) {
	const vm = new NodeVM(options);
	try {
		Object.assign(vm._resolver ?? {}, { packageCache, scriptCache });
	} catch { /* This is currently a hack! */ }
	const handler = vm.run(script);
	return handler(args);
}

This shares the internal package and script caches for require between the instances. Getting the performance better than this will likely not be possible.

from vm2.

XmiliaH avatar XmiliaH commented on June 12, 2024 1

They will not leak any data between vms. It only caches the compiled VMScripts for modules so that hopefully require will be a bit faster.

from vm2.

theinterned avatar theinterned commented on June 12, 2024 1

As a follow up: @XmiliaH , your optimization has been a HUGE perf win for us. Using pretty much exactly your code, I am able to get the time to create a fresh render context from ~1.5s all the way down to ~100ms! 🚀

I think this makes sense as the script we're requiring in our VM is in fact an entire webpack bundle of react code, so avoiding re-linking everything is a major win.

Thank you so much this is awesome!

One question I have about this: this _resolver definitely seems to be a private API: it's not exposed on the NodeVM type. Would you consider changing this and making a public API for this?

from vm2.

XmiliaH avatar XmiliaH commented on June 12, 2024 1

Thanks for the feedback. Yes _resolver is a private API. I will look into making a public API for this.

from vm2.

XmiliaH avatar XmiliaH commented on June 12, 2024 1

@theinterned Could you take a look at #519 if it would fit your usecase.

from vm2.

theinterned avatar theinterned commented on June 12, 2024

Oh thanks wow 🤩! I will give this a try. Will sharing the packageCache and scriptCache leak any data between vms?

from vm2.

theinterned avatar theinterned commented on June 12, 2024

Oh #519 looks very interesting! I ran out of time to try it out this week, but I'll mess around with it next week for sure. Thanks you! ⭐⭐⭐⭐⭐

from vm2.

manuelpuyol avatar manuelpuyol commented on June 12, 2024

hey @XmiliaH, do you know if the _resolver cache would also impact dynamic imports?

from vm2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.