Check Global Object Pollution using global-object-guard

06 / Sep / 2014 by Kashish Gupta 0 comments

JavaScript is language for Ninjas some time mistake happens by Ninja also which slow down our Node Application, because unknowingly we are dynamically creating the property on GLOBAL objects or some time we get stuck in some closure issue’s which are not easy to trace and becomes a memory leak for the application. These are the common problem’s with Ninjas. During all these problems I found some useful npm module called global-object-guard which become a solution for my problems related with GLOBAL objects.

Lets discuss GLOBAL issues with the help of example.

_appSetting = {
    PORT: 2000,
    HOST: "http://localhost"
};

_otherServerSetting = {
    //So on...
};

// Do your code .......

obj = {name: "Kashish", age: 24};

(function () {
    person = {address: "B4-90 US"}
})();

The above code polluting the GLOBAL object by making property dynamically which can be unknown to the developer

When I write same code using npm module global-object-guard. Lets see how it will help us out to check whether code is hitting global object or not??

Help your self with global-object-guard

// Require this Package
var GlobalObjectGuard = require('global-object-guard');

//My All Global Object Settings
_appSetting = {
    PORT: 2000,
    HOST: "http://localhost"
};

_otherServerSetting = {
    //So on...
};
//--------------------------------------------

// Just Apply GlobalObjectGuard
var gb = GlobalObjectGuard.Create(global);

// Do your code .......

// Some time Mistakes happen
obj = {name: "Kashish", age: 24};

// Don't worry just check the Global Pollution if you want.
var result = gb.getDifference();
console.log("Checking Global Object Pollution: ", result);
// Output: Checking Global Object Pollution:  { obj: [ { name: 'Kashish', age: 24 } ] }

// You can reposition your global object state
// This will Set your current global state as a default State
// (NOTE: ALL THE GLOBAL POLLUTION BECOMES THE PART OF GLOBAL OBJECT)
gb.rePositionGlobalObject(global);

// Recheck the Status
result = gb.getDifference();
console.log("After Reposition the Global Object: " + JSON.stringify(result));
// Output: After Reposition the Global Object: {}

// Lets do some mistake
(function () {
    person = {address: "B4-90 US"}
})();

result = gb.getDifference();
console.log("Opes Just hit the global object: " + JSON.stringify(result));
// Output: Opes Just hit the global object: {"person":[{"address":"B4-90 US"}]}

// Let this GlobalObjectGuard take care this cleaning work. [ Dangerous it can kill your app also ]
gb.refreshGlobalObject();

// Check the status. Every Thing is Clean Now.
result = gb.getDifference();
console.log("Everything is clean by GlobalObjectGuard: " + JSON.stringify(result));
// Output: Everything is clean by GlobalObjectGuard: {}

Some useful link…

NPM: https://www.npmjs.org/package/global-object-guard

GitHub: https://github.com/kashishgupta1990/global-object-guard

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *