Skip to content

Instantly share code, notes, and snippets.

@bichotll
Created April 30, 2014 11:15
Show Gist options
  • Save bichotll/c6701673764950e558f5 to your computer and use it in GitHub Desktop.
Save bichotll/c6701673764950e558f5 to your computer and use it in GitHub Desktop.
javascript private methods benchmark
/*
The aim of this test is to check the performance (as speed) and the memory creating instances of objects with public or private methods.
We want to see if create a private method in js really hurts the performance.
For that we will compare the performance creating so many instances and checking the performance
*/
function Public(){
this.publicMethod = function(){
};
}
function Private(){
var privateMethod = function(){
}
}
console.time("Private method");
var array = new Array(1000000);
for (var i = array.length - 1; i >= 0; i--) {
array[i] = new Private();
};
console.timeEnd("Private method");
console.time("Public method");
var array = new Array(1000000);
for (var i = array.length - 1; i >= 0; i--) {
array[i] = new Public();
};
console.timeEnd("Public method");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment