Skip to content

Commit

Permalink
async_hooks: faster AsyncResource#bind()
Browse files Browse the repository at this point in the history
  • Loading branch information
bengl committed May 12, 2022
1 parent 8e0f3ff commit 003b50b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
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', {
configurable: true,
enumerable: false,
value: fn.length,
writable: false,
});
bound.asyncResource = this;
return bound;
}

Expand Down

0 comments on commit 003b50b

Please sign in to comment.