Skip to content

Instantly share code, notes, and snippets.

@peta
Created February 21, 2010 16:26
Show Gist options
  • Save peta/310397 to your computer and use it in GitHub Desktop.
Save peta/310397 to your computer and use it in GitHub Desktop.
var repeat = 500;
// Using Crockford's Module Pattern
console.profile("Crockford's Module Pattern");
var arrObjLit = [],
iObjLit = repeat;
while (iObjLit--) {
(function() {
var privVar = "privVal",
privVar2 = "privVal2",
privVar3 = "privVal3";
function privMeth() {
//Foo
}
this.push({
pubMeth: function() {
privMeth(privVar);
}
});
}).call(arrObjLit);
}
while (++iObjLit < repeat) {
arrObjLit[iObjLit].pubMeth();
}
console.profileEnd();
// Instantiation of anonymous function instances
console.profile("anonymous function instances");
var arrAnonFn = [],
iAnonFn = repeat;
while (iAnonFn--) {
arrAnonFn.push(new function() {
var privVar = "privVal",
privVar2 = "privVal2",
privVar3 = "privVal3";
function privMeth() {
//Foo
}
this.pubMeth = function() {
privMeth(privVar);
};
});
}
while (++iAnonFn < repeat) {
arrAnonFn[iAnonFn].pubMeth();
}
console.profileEnd();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment