diff --git a/src/common.js b/src/common.js index 2f82b8dc..56b7737d 100644 --- a/src/common.js +++ b/src/common.js @@ -19,8 +19,9 @@ function setup(env) { /** * Active `debug` instances. + * @type {Object} */ - createDebug.instances = []; + createDebug.instances = {}; /** * The currently active debug mode names, and names to skip. @@ -63,6 +64,11 @@ function setup(env) { */ function createDebug(namespace) { let prevTime; + const value = createDebug.instances[namespace]; + + if (value !== undefined) { + return value; + } function debug(...args) { // Disabled? @@ -74,8 +80,7 @@ function setup(env) { // Set `diff` timestamp const curr = Number(new Date()); - const ms = curr - (prevTime || curr); - self.diff = ms; + self.diff = curr - (prevTime || curr); self.prev = prevTime; self.curr = curr; prevTime = curr; @@ -128,15 +133,14 @@ function setup(env) { createDebug.init(debug); } - createDebug.instances.push(debug); + createDebug.instances[namespace] = debug; return debug; } function destroy() { - const index = createDebug.instances.indexOf(this); - if (index !== -1) { - createDebug.instances.splice(index, 1); + if (createDebug.instances[this.namespace] !== undefined) { + delete createDebug.instances[this.namespace]; return true; } return false; @@ -180,8 +184,10 @@ function setup(env) { } } - for (i = 0; i < createDebug.instances.length; i++) { - const instance = createDebug.instances[i]; + const keys = Object.keys(createDebug.instances); + + for (i = 0; i < keys.length; i++) { + const instance = createDebug.instances[keys[i]]; instance.enabled = createDebug.enabled(instance.namespace); } }