Skip to content

Instantly share code, notes, and snippets.

@srhyne
Created October 29, 2011 17:30
Show Gist options
  • Save srhyne/1324823 to your computer and use it in GitHub Desktop.
Save srhyne/1324823 to your computer and use it in GitHub Desktop.
JS Perf Notes closures & anon functions
// avoid using anon function cb's for high touch events
$("div").mousemove(function(){
//this function is re-created every time w/ obvious overhead
});
//avoid re-creating Class methods in a constructor pattern. Use prototype instead
//instead of this...
function Person(first, last){
this.first_name = first;
this.last_name = last;
//methods
this.getFullName = function(){
return this.first_name+ " " + this.last_name;
}
}
//put getFullName in the prototype
Person.prototype.getFullName = function() {
return this.first_name+ " " + this.last_name;
};
// I would say the module pattern with private vars is fine when
// initiating a class but for Factory'esque modules/classes reused a lot avoid closures
// If you need private vars for these high touch classes just use this._ convention
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment