Skip to content

Redundant iterations in d3.dispatch.call when invoking event callbacks #38

@timmacp

Description

@timmacp

Problem:
In d3.dispatch.call within D3 v6, the loop over t = this._[type] iterates more times than necessary,
creating confusion and potential inefficiency.
For a single callback, multiple loop iterations (sometimes as many as six) are observed.

Cause:
The loop reuses variable names (n and t), which leads to unclear iteration behavior,
although it does not cause incorrect functionality.

Suggested Improvement:
Replace the loop with a more straightforward version using different variable names and simplified iteration:

for (let j = 0, len = callbacks.length; j < len; j++) {
  callbacks[j].value.apply(that, args);
}

This would enhance readability and maintainability without altering behavior.

Impact:
Clearer, more maintainable code
Possible minor performance improvement

Question:
Is this optimization feasible across contexts, or is the original design required for compatibility?

Second improvement:
While both ++i and i++ work functionally the same in a for loop,
the use of i++ (post-increment) would improve clarity and readability
by more closely following the natural flow of iteration and increment: first process, then increment.
It's the most common form for a for loop, in many programming languages
(including C, C++, Java, etc.), for that reason.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions