From 70de43727d191c10d8ba9542bd0cdabaf272360a Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 1 Jul 2023 23:01:59 -0500 Subject: [PATCH] [Performance] use `call-bind` for autobinding This avoids a stack overflow with lots of nested tests --- lib/test.js | 8 ++------ test/async-await.js | 2 -- test/teardown.js | 2 ++ 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/test.js b/lib/test.js index f4013bc1..d19e9b71 100644 --- a/lib/test.js +++ b/lib/test.js @@ -8,6 +8,7 @@ var EventEmitter = require('events').EventEmitter; var has = require('has'); var isRegExp = require('is-regex'); var trim = require('string.prototype.trim'); +var callBind = require('call-bind'); var callBound = require('call-bind/callBound'); var forEach = require('for-each'); var inspect = require('object-inspect'); @@ -91,12 +92,7 @@ function Test(name_, opts_, cb_) { var self = this; for (var prop in self) { if (typeof self[prop] === 'function') { - self[prop] = (function bind(propName) { - var val = self[propName]; - return function bound() { - return val.apply(self, arguments); - }; - }(prop)); + self[prop] = callBind(self[prop], self); } } } diff --git a/test/async-await.js b/test/async-await.js index 6b014a87..f5383a2a 100644 --- a/test/async-await.js +++ b/test/async-await.js @@ -210,9 +210,7 @@ tap.test('sync-error', function (t) { '', 'Error: oopsie', ' at Test.myTest ($TEST/async-await/sync-error.js:$LINE:$COL)', - ' at Test.bound [as _cb] ($TAPE/lib/test.js:$LINE:$COL)', ' at Test.run ($TAPE/lib/test.js:$LINE:$COL)', - ' at Test.bound [as run] ($TAPE/lib/test.js:$LINE:$COL)', node17 ? [ '', 'Node.js ' + process.version diff --git a/test/teardown.js b/test/teardown.js index b213853c..691c66be 100644 --- a/test/teardown.js +++ b/test/teardown.js @@ -40,6 +40,8 @@ tap.test('teardowns', function (tt) { ' [... stack stripped ...]', ' at Test. ($TEST/teardown.js:$LINE:$COL)', ' [... stack stripped ...]', + ' at Test. ($TEST/teardown.js:$LINE:$COL)', + ' [... stack stripped ...]', ' ...', '# nested teardown (nested fail level)', '# nested teardown (nested fail level) 2',