Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement of memory management for #678 #699

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/common.js
Expand Up @@ -19,8 +19,9 @@ function setup(env) {

/**
* Active `debug` instances.
* @type {Object<String, Function>}
*/
createDebug.instances = [];
createDebug.instances = {};

/**
* The currently active debug mode names, and names to skip.
Expand Down Expand Up @@ -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?
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down