Assertions Removal

Potency None
Resilience High
Cost None
Tags: assertion, elimination, removal, optimization, assert

Description

An assertion is a statement that contains a test predicate (a true-false expression) which is expected to always be true, otherwise the assertion will fail. Typically, assertions are turned off when the code is deployed to production to avoid any side effects and reduce performance impact. This transformation can be used to remove all assertions from the production code including their function declarations.

Code Annotation Example

// @jscrambler define assertionsRemoval {names: [expect,assert]} as ar
// @jscrambler enable ar

Example

The following example contains some assertions that will be removed by the transformation:

function assert(result, description) {
  // assert code
}

function a() {
  // code block
}

function b() {
  // code block
}

function c() {
  // code block
}
var d = 1;
assert(d === 1);
d += 2;
assert(d > 1, 'd is greater than 1');

Adding assert to the names list removes any function declaration and function call with that name:

function a() {
  // code block
}

function b() {
  // code block
}

function c() {
  // code block
}
var d = 1;
d += 2;

Option Types

Name Required Default Value Description
names Yes [] List containing the names of the assertions to be removed. Each name can be provided as a string or as a regular expression.

Browser Compatibility

Browser Compatible Versions Tested Versions Notes
Chrome 80+ 80+
Firefox 90+ 90+
Internet Explorer 8+ 8+
Microsoft Edge 116+ 116+
Safari 13.1+ 13.1+

API Parameters

Example:

{
  "keys": {
    "accessKey": "XXXXXX",
    "secretKey": "YYYYYY"
  },
  "applicationId": "ZZZZZZ",
  "params": [
    {
      "name": "assertionsRemoval",
      "options": {
        "names": []
      }
    }
  ]
}