3
votes

I was just curious, looking around, it seems that Javascript does not have a equals() method like Java. Also, neither == or === can be used to check iff the two operators are the same item. So how is it that Clojurescript has a == and a identical? operator?

Also, should I expect identical? to be substantially faster than == in Clojurescript?

2

2 Answers

3
votes

Here's a quick result from the Himera ClojureScript REPL:

    cljs.user> =
    #<function (a, b) {
    return cljs.core._equiv.call(null, a, b)
    }>

    cljs.user> ==
    #<function (a, d, e) {
    switch(arguments.length) {
    case 1:
    return!0;
    case 2:
    return cljs.core._equiv.call(null, a, d);
    default:
    return b.apply(this, arguments)
    }
    throw"Invalid arity: " + arguments.length;
    }>

    cljs.user> identical?
    #<function (a, b) {
    return a === b
    }>

According to Mozilla's JavaScript Reference on Comparison Operators the === operator does compare to see if the two operands are the same object instance, and since identical? in clojurescript maps directly onto === in JavaScript it will therefore do just that.

The fact that identical? maps directly onto a === b would also suggest that it'll be significantly faster than = or == since they both translate to calls to cljs.core._equiv. However, it wouldn't surprise me if a good JavaScript JIT engine reduced all three to very similar machine code for numbers since the -equiv implementation for numbers just maps onto identical?:

(extend-type number
  IEquiv
  (-equiv [x o] (identical? x o))
3
votes