diff --git a/benchmark/async_hooks/async-resource-bind.js b/benchmark/async_hooks/async-resource-bind.js new file mode 100644 index 00000000000000..bfc4ab436d15cc --- /dev/null +++ b/benchmark/async_hooks/async-resource-bind.js @@ -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); +} diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 6cc2052474d9c2..df181b02532176 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -8,7 +8,7 @@ const { ArrayPrototypeUnshift, FunctionPrototypeBind, NumberIsSafeInteger, - ObjectDefineProperties, + ObjectDefineProperty, ObjectIs, ReflectApply, Symbol, @@ -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; }