lukejacksonn / perflink

Low friction JavaScript benchmarks that you can share via URL
https://perf.link
1.21k stars 43 forks source link

Is there a way to require modules? #13

Closed joshuaquek closed 4 years ago

joshuaquek commented 5 years ago

Let's say that I want to import lodash and underscore just to compare each one's performance, is there a way to do it?

I've tried:

import _ from 'lodash'

and

const _ = require('lodash')

but neither work (obviously).

Is there any way to include external modules?

Lodash is also available minified here: https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js

...maybe we can allow including of external modules?

lukejacksonn commented 5 years ago

So I had a go at this myself (as it is something I'd like to support) and it turns out that you cannot use the import keyword inside of eval() or new Function(). But what we can do instead (and this might sound crazy but it works) is base64 encode the setup code and test cases, then use that as a data url for an iframe.

This will allow us to use both static and dynamic imports and help sandbox test cases.

lukejacksonn commented 4 years ago

This is now supported (was included in #24) thanks to worker type module which landed in chrome recently. You can now use top level await or es-module import syntax.

Hope this helps, apologies it took so long to implement! 🙇