As of v0.26, k6 supports running test scripts with different ECMAScript compatibility modes using the run --compatibility-mode CLI option or K6_COMPATIBILITY_MODE environment variable.
Currently two modes are available:
Your mileage may vary while running --compatibility-mode=base and also importing external dependencies. For instance, xml2js and cheerio currently do not work, while lodash does.
Note that require() is a custom k6 implementation of module loading, which doesn't behave in the same way as the require() call in Node.js. Specifically, it only handles loading of built-in k6 modules, scripts on the local filesystem, and remote scripts over HTTP(S), but it does not support the Node.js module resolution algorithm.
In case of syntax/parsing errors, the script will be transformed using Babel with specific plugins bringing the compatibility to ES2015(ES6)+. This means that features such as classes and arrow functions can be used. This does take some time to transpile and the produced code has slightly different line/column numbers.
Before k6 v0.31.0, core.js v2 and even more Babel plugins were also included in extended mode. This added around 2MB extra memory usage per VU and some of the transformations (generators, async/await) of Babel were not useful as they were still not enough for k6 to just work with those features. So, before v0.31.0, using --compatibility-mode=base was a significant improvement on memory usage, which also translated to some CPU gains.
There's a substantial difference in performance between both modes, as shown by GNU time below, especially when running tests with a large number of VUs: