nodejs / nodejs/node

[V8 perf] Loop callback changing is throttling

Open
#65,894 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Version

v24.20.0

Platform
Windows 10
Subsystem

No response

What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?
  • BOX (~5 secs)
  • BOX (~5 secs)
  • LANCZOS (~8 secs)
  • LANCZOS (~8 secs)
What do you see instead?
  • BOX (~5 secs)
  • BOX (~5 secs)
  • LANCZOS (~30 secs)
  • LANCZOS (~30 secs)
Additional information

I have downsample1D_V1 function (it's very unoptimized, but it works, and it's perfect for the demonstration), which executes the passed callback const weight = fn(x); billions of times (3_107_687_040 times per 1 downsample2D call).

There are 2 types of the passed callback function — lanczos3 and box:

function lanczos3(x: number): number {
  if (-3 <= x && x < 3) {
    return sinc(x) * sinc(x / 3);
  }
  return 0;
}

function sinc(x: number): number {
  if (x === 0) {
    return 1;
  }
  x = Math.PI * x;
  return Math.sin(x) / x;
}


function box(x: number): number {
  if (x > -0.5 && x <= 0.5) {
    return 1;
  }
  return 0;
}

I call downsample2D multiple times with the follow callback params (demo-1.ts):

  • BOX (~5 secs)
  • BOX (~5 secs)
  • LANCZOS (~30 secs)
  • LANCZOS (~30 secs)

The first 2 calls work with the expected speed, for ~5 second.
But when the callback is changed, downsample2D works multiple times slower than expected, for ~30 seconds.

In reverse order I also face the throttling (demo-2.ts):

  • LANCZOS (~8 secs)
  • LANCZOS (~8 secs)
  • BOX (~30 secs)
  • BOX (~30 secs)

The problem appears on both Node.js (24) and Deno (2.9).

Bun (1.3/1.4) works fine:

  • BOX (~5 secs)
  • BOX (~5 secs)
  • LANCZOS (~8 secs)
  • LANCZOS (~8 secs)

Related keywords: performance v8 polymorphic inline cache

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by cloning the linked reproduction and running demo-1.ts and demo-2.ts under Node.js v24, comparing the callback-switching timings. Investigate the reported V8 polymorphic inline-cache behavior and verify the result against the BOX and LANCZOS cases. Done means changing callbacks no longer causes the repeated downsample2D calls to slow from the expected timings.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.