Is there any difference in performance between the conditional operator ===
and the assignment operator =
? I am writing some pre-save hook middleware in mongoose and I am wondering if there is much of a speed difference between:
UserSchema.pre('save', function (next) {
if (!this.isModified()) {
return next();
}
this.crm.isUpToDate = false;
next();
});
and
UserSchema.pre('save', function (next) {
if (!this.isModified()) {
return next();
}
if (this.crm.update === true) {
this.crm.isUpToDate = false;
}
next();
});
EDIT:
Thanks for the constructive comments.
Basically, it doesn't look like there is much of a difference in the performance (as stated above it's negligible). Thanks for the cool tool for testing speed http://jsperf.com/, I had never heard of it before.
For those of you who are wondering about the code, first off I made a blatant error in my original post, then when everyone tried to point it out to me I cried, that's probably the reason why everyone downvoted.
Here is what I am doing:
I have a mongoose pre-save middleware hook (for a mongo database) where the hook gets run every time a document is saved. At the point of save I check if the document was updated. If it was I set the crmIsUpToDate
to false. The crmIsUpToDate
will get set to true when a cron job gets. This hook can be run many times before the cron job gets to the document.
I didn't think this was all that necessary for the question because the question was if there is a difference between doing a comparison ===
and doing an assignment =
. I shouldn't have even put the code up because it really detracted from the main question.