Skip to content

Commit

Permalink
[Performance] use call-bind for autobinding
Browse files Browse the repository at this point in the history
This avoids a stack overflow with lots of nested tests
  • Loading branch information
ljharb committed Jul 2, 2023
1 parent 2043b2e commit 70de437
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 8 deletions.
8 changes: 2 additions & 6 deletions lib/test.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions test/async-await.js
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/teardown.js
Expand Up @@ -40,6 +40,8 @@ tap.test('teardowns', function (tt) {
' [... stack stripped ...]',
' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
' [... stack stripped ...]',
' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
' [... stack stripped ...]',
' ...',
'# nested teardown (nested fail level)',
'# nested teardown (nested fail level) 2',
Expand Down

0 comments on commit 70de437

Please sign in to comment.