Given the code:
function foo() {
if (!this.isValid) {
this.isValid = true;
}
}
and if I have foo() triggered by an resize event, is it any less efficient to do this:
function foo() {
this.isValid = true;
}
where it would be constantly setting this.isValid = true . The end result is the same but it's just resetting the variable over and over. Is there a difference memory wise to either one?
There wont be significant difference in both of them. As in the case of if, you ll have a comparison added. Incase of direct resetting of the value. you ll skip the comparison but the assignment operator will take up some time.
This can help for a cleaner code
function foo() {
this.isValid = this.isValid || true;
}
// so you value will not be overwritten