Skip to content

Conversation

@jdmarshall
Copy link
Collaborator

Fixes #135

// Deserialize the benchmark function
function deserializeBenchmark(benchmark) {
benchmark.fn = new Function(benchmark.fn);
benchmark.fn = new AsyncFunction("timer", benchmark.fn);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my research, adding unnecessary await async to a sync function reduces the performance and increases the variability of the benchmarked function by a significant amount.

Can you please test with and without your patch the ops/sec of examples/worker-threads/node.js?

Copy link
Collaborator Author

@jdmarshall jdmarshall Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well you’re stripping off the function signature on the sending side, so there’s no clean was to know unless we inspect it there first.

Found this while looking at another PR:

https://github.com/RafaelGSS/bench-node/blob/main/lib/index.js#L74

this.isAsync = types.isAsyncFunction(this.fn);

I’ll see what I make of that.

Copy link
Collaborator Author

@jdmarshall jdmarshall Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a block of code in clock.js that does similar logic. I've added both an async and a hasArg check which should retain as much throughput as can be maintained. Unfortunately my library will hit both unless I do some more special casing in the function template which I really do not like.

Emulate the same optimizations from clock.js in worker-runner.js.

This will only create an async function if the origin function is
async, and will only trigger the Timer logic flow if the origin
function contained the same.
Copy link
Owner

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better!

@RafaelGSS RafaelGSS merged commit 35eb144 into RafaelGSS:main Dec 9, 2025
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

worker threads cannot access the timer object

2 participants