Skip to content
Advertisement

What’s the meaning of /*#__PURE__*/ in some javascript source code?

for example:

function _test() {
  _test = _asyncToGenerator(
    /*#__PURE__*/ regeneratorRuntime.mark(function _callee(args) {
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch ((_context.prev = _context.next)) {
            case 0:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })
  );
  return _test.apply(this, arguments);
}

I both have tried google and baidu, but nothing helpful for me.

Advertisement

Answer

It’s an indication that the function in question is pure, or to be more precise, side-effect free. This helps with tree-shaking – the removal of dead code from bundles when nothing else references a particular value.

Just for example, if you have a function available, foo, and the result of calling foo isn’t ever used in the project, eg

const x = 3;
foo();
const y = 4;

and foo is pure, then the line there doesn’t do anything.

If all such calls to foo are never used, then code optimizers are free to remove the foo function completely (as well as everywhere it’s called, if all such calls do not have their return value used).

Advertisement