Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async_hooks: faster AsyncResource#bind() #43065

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions benchmark/async_hooks/async-resource-bind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const common = require('../common.js');
const { AsyncResource } = require('async_hooks');

const noop = () => {};
const thisArg = {};
const resource = new AsyncResource('test');

const bench = common.createBenchmark(main, {
n: [1e100],
option: ['withThisArg', 'withoutThisArg']
});

function main({ n, option }) {
bench.start();
for (let i = 0; i < 1e7; i++) {
resource.bind(noop, null, option === 'withThisArg' ? thisArg : undefined);
}
bench.end(n);
}
21 changes: 7 additions & 14 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
ArrayPrototypeUnshift,
FunctionPrototypeBind,
NumberIsSafeInteger,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectIs,
ReflectApply,
Symbol,
Expand Down Expand Up @@ -236,20 +236,13 @@ class AsyncResource {
} else {
bound = FunctionPrototypeBind(this.runInAsyncScope, this, fn, thisArg);
}
ObjectDefineProperties(bound, {
'length': {
configurable: true,
enumerable: false,
value: fn.length,
writable: false,
},
'asyncResource': {
configurable: true,
enumerable: true,
value: this,
writable: true,
}
ObjectDefineProperty(bound, 'length', {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just stop doing this completely? This is only one of the cases needed to preserve the original functionality as the function could also have other properties or symbols attached to it for example, and those wouldn't be copied. Maybe the best approach then would be to copy nothing and leave it to the user to decide what needs copying. Then the implementation will be as fast as it can be and will only become slower if the user decides that copying properties is needed.

configurable: true,
enumerable: false,
value: fn.length,
writable: false,
});
bound.asyncResource = this;
return bound;
}

Expand Down