Repository: eslint/eslint
File: lib/rules/no-sparse-arrays.js (JavaScript)

Code before:
    meta: {
        type: "problem",

        docs: {
            description: "Disallow sparse arrays",
            recommended: true,
            url: "https://eslint.org/docs/latest/rules/no-sparse-arrays"
        },

        schema: [],

        messages: {
            unexpectedSparseArray: "Unexpected comma in middle of array."
        }
    },

    create(context) {


        //--------------------------------------------------------------------------
        // Public
        //--------------------------------------------------------------------------

        return {

            ArrayExpression(node) {
                const { sourceCode } = context;
                let commaToken;


                for (const element of node.elements) {
                    commaToken = sourceCode.getTokenAfter(
                        element ?? commaToken ?? sourceCode.getFirstToken(node),
                        astUtils.isCommaToken
                    );

                    if (element) {
                        continue;
                    }

                    context.report({
                        node,
                        loc: commaToken.loc,
                        messageId: "unexpectedSparseArray"
                    });
                }
            }

        };

    }

Review comment (suggestion):
Perhaps we could keep this quick check for an early exit (`if (!node.elements.includes(null)) { return; }`) since the rule now takes considerably more time due to searching for every comma?

This is TIMING for this repo before & after the change:

```
before:

no-sparse-arrays                                        |     5.912 |     0.0%

after:

no-sparse-arrays                                        |    49.170 |     0.2%

``` 

Code after:
    meta: {
        type: "problem",

        docs: {
            description: "Disallow sparse arrays",
            recommended: true,
            url: "https://eslint.org/docs/latest/rules/no-sparse-arrays"
        },

        schema: [],

        messages: {
            unexpectedSparseArray: "Unexpected comma in middle of array."
        }
    },

    create(context) {


        //--------------------------------------------------------------------------
        // Public
        //--------------------------------------------------------------------------

        return {

            ArrayExpression(node) {
                if (!node.elements.includes(null)) {
                    return;
                }

                const { sourceCode } = context;
                let commaToken;

                for (const [index, element] of node.elements.entries()) {
                    if (index === node.elements.length - 1 && element) {
                        return;
                    }

                    commaToken = sourceCode.getTokenAfter(
                        element ?? commaToken ?? sourceCode.getFirstToken(node),
                        astUtils.isCommaToken
                    );

                    if (element) {
                        continue;
                    }

                    context.report({
                        node,
                        loc: commaToken.loc,
                        messageId: "unexpectedSparseArray"