Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Object.is(x,y) faster than other comparisons?

So in JavaScript we have three equality comparison operators. I've been reading the ECMAScript specification and looking at how they work. Something struck me. The Object.is() built in function has less steps to it's comparison and it has a greater chance of terminating early than the other operators. So does this make Object.is() function faster than the other operators?

Here are the specification snippets:

Object.is()

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Number, then

    a. If x is NaN and y is NaN, return true.

    b. If x is +0 and y is -0, return false.

    c. If x is -0 and y is +0, return false.

    d. If x is the same Number value as y, return true.

    e. Return false.

  3. Return SameValueNonNumber(x, y).

Loose equals ==

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is the same as Type(y), then

    a. Return the result of performing Strict Equality Comparison x === y.

  2. If x is null and y is undefined, return true.
  3. If x is undefined and y is null, return true.
  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ! ToNumber(y).
  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ! ToNumber(x) == y.
  6. If Type(x) is Boolean, return the result of the comparison ! ToNumber(x) == y.
  7. If Type(y) is Boolean, return the result of the comparison x == ! ToNumber(y).
  8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
  9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y.
  10. Return false.

Strict equals ===

The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is different from Type(y), return false.

  2. If Type(x) is Number, then

    a. If x is NaN, return false.

    b. If y is NaN, return false.

    c. If x is the same Number value as y, return true.

    d. If x is +0 and y is -0, return true.

    e. If x is -0 and y is +0, return true.

    f. Return false.

  3. Return SameValueNonNumber(x, y).

If someone who works on JavaScript compilers could answer that question, that would be great!

like image 745
Boško Bezik Avatar asked Sep 07 '25 01:09

Boško Bezik


1 Answers

Less steps in the spec do not necessarily mean less steps in the implementation. All kinds of optimizations apply here. So in the end, all we can do is to actually race the horses! (in your real world usecase, some synthetic while(true) Object.is(1, 1); won't generate any useful results). If it is really (notably) faster, does it make your code better, e.g. is

      Object.is(user.age, 3)

clearer than

      user.age === 3

?

like image 84
Jonas Wilms Avatar answered Sep 10 '25 13:09

Jonas Wilms