Skip to content

Instantly share code, notes, and snippets.

@normanzb
Created November 2, 2011 01:54
Show Gist options
  • Save normanzb/1332642 to your computer and use it in GitHub Desktop.
Save normanzb/1332642 to your computer and use it in GitHub Desktop.
Memory Usage Comparison - OO
var ObjectContainer = [];
var HOW_MANY_OBJ = 100000;
var memoryHolder;
function Heavy (){
this.load = function(){
var str =
'gabagesgabagesgabagesgabagesgabages';
for(var i = 0; i < 9999; i++){
str+=str;
}
this._loads = str;
};
this.release = function(){
this._loads = null;
}
}
function Light(){
}
var p = Light.prototype;
p.load = function(){
var str =
'gabagesgabagesgabagesgabagesgabages';
for(var i = 0; i < 9999; i++){
str+=str;
}
this._loads = str;
};
p.release = function(){
this._loads = null;
}
console.log('Creating Heavy objects...');
memoryHolder = console.memory.usedJSHeapSize;
console.log(console.memory);
console.time('Heavy');
for(var i = 0; i < HOW_MANY_OBJ; i++){
ObjectContainer.push(new Heavy);
}
console.timeEnd('Heavy');
console.log(console.memory);
console.log("Used JS Heap: " + (console.memory.usedJSHeapSize - memoryHolder));
console.log('Creating Light objects...');
memoryHolder = console.memory.usedJSHeapSize;
console.log(console.memory);
console.time('Light');
for(var i = 0; i < HOW_MANY_OBJ; i++){
ObjectContainer.push(new Light);
}
console.timeEnd('Light');
console.log(console.memory);
console.log("Used JS Heap: " + (console.memory.usedJSHeapSize - memoryHolder));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment