Which is faster: foo.constructor === Constructor or foo.constructor.name === "Constructor"?


I never knew which is faster: foo.constructor === Constructor or foo.constructor.name === "Constructor" (foo is not null or undefined)?

Let's take an example:

var obj = {};

if (obj.constructor === Object) {
     /* do something */

if (obj.constructor.name === "Object") {
     /* do something */

Which is faster?

I would prefer to use obj.constructor === Object just because it's shorter, but is there any reason for what I must use obj.constructor.name === "Object"? Is it faster than obj.constructor === Object?

Problem courtesy of: Ionică Bizău


Apparently the first one is faster (link to jsperf), at least in Chrome 31. (The next time you have a performance question like this, JSPerf can be very helpful.)

This seems right, since you don't have to create a string in the first one, and comparing strings is naturally slower than comparing objects anyway.

However, this is really micro-optimization. Will a user really notice a difference? (No.) Use whatever you think is more readable.

Solution courtesy of: Doorknob


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.