You can run test scripts with different ECMAScript compatibility modes with the run --compatibility-mode CLI option or K6_COMPATIBILITY_MODE environment variable.
Currently two modes are available:
Base
Pure Golang JavaScript VM supporting ES5.1+. Use this mode if your scripts are already written using only ES5.1 features, or were previously transformed by Babel, to reduce startup time, RAM usage and improve performance. See the k6-es6 project for an example Webpack setup that does this transformation outside of k6.
⚠️ Disclaimer
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.
Basic Example
⚠️ About require()
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.
Advanced Example
Extended
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 v0.31.0, k6 included core.js v2 and even more Babel plugins in extended mode. This added around 2MB extra memory usage per VU and some of the transformations (generators, async/await) of Babel were still insufficient to get k6 working with these features.
Performance Comparison
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: