Issue Task Closed

#106201Improve code readability JS Array.prototype.reduce

Ayke Halder 02/18/25, 13:28 updated 11/13/26, 08:24

Description

const foo = [1, 2, 3];

// prints [ 1, 2, 3 ]
console.log(foo);

const bar = foo.reduce(
  (foo /* <-- better name this 'fooAccumulator' */, node) => {
    foo.push(node + 'reduced');
    return foo;
  },
  []
);

// prints [ 1, 2, 3 ]
console.log(foo);

// prints [ '1reduced', '2reduced', '3reduced' ]
console.log(bar);

Reviews 2

History 46

  1. Ayke Halder changed subject Improve code readability JS Object.reduce Improve code readability JS Array.prototype.reduce <span data-processor="relativetime" data-value="2025-02-18T14:27:35+00:00" > 02/18/25, 14:27 </span>

  2. Georg Ringer commented and changed status Under Review Closed <span data-processor="relativetime" data-value="2026-11-12T21:02:33+00:00" > 11/12/26, 21:02 </span>

    Closing this one as won’t do.

    The change in Gerrit (https://review.typo3.org/c/Packages/TYPO3.CMS/+/88264) has been abandoned. Using a unique name for the accumulator parameter of Array.prototype.reduce is a matter of taste, and the no-shadow rule of typescript-eslint that would enforce it is not enabled in our ESLint configuration. The additional switch from spread to push() only pays off for very large node lists and has been objected to in the review. The discussion is documented in the Gerrit change.

    If this is still valid for you in v15, please open a new issue and provide information how to reproduce it. Thanks!

    hope you understand

  3. Ayke Halder commented <span data-processor="relativetime" data-value="2026-11-13T08:24:24+00:00" > 11/13/26, 08:24 </span>

    Georg Ringer wrote in #note-4:
    > Using a unique name for the accumulator parameter of Array.prototype.reduce is a matter of taste, and the no-shadow rule of typescript-eslint that would enforce it is not enabled in our ESLint configuration.

    I’m fine! This is just a decissio to make.

    > The additional switch from spread to push() only pays off for very large node lists and has been objected to in the review. The discussion is documented in the Gerrit change.

    About the very large node lists the algorithm implemented to enhance the nodes is computational expensive. Thereby for every node all the nodes in the array are “searched” to find the correct parent node…

    With N nodes this becomes “N” searches over “N to 1” nodes until hit:
    N * 0.5 N searches in total —> complexity O(n²)

    So even not having that many nodes makes the algorithm go slow.

    For improvement see comment:
    https://review.typo3.org/c/Packages/TYPO3.CMS/+/91284/comments/d11daad0_78c4b269