+new Date() - Is this good practice?

前端 未结 5 1241
一整个雨季
一整个雨季 2020-12-12 22:37

So we had the Discussion today in our company about +new Date() being good practice or not. Some prefer this way over new Date().getTime().

5条回答
  •  不知归路
    2020-12-12 23:04

    The getTime method appears to be a huge amount faster:

    enter image description here

    Why is this the case?

    Here's what happens when you call the getTime method on a Date instance:

    1. Return the value of the [[PrimitiveValue]] internal property of this Date object.

    Here's what happens when you apply the unary plus operator to a Date instance:

    1. Get the value of the Date instance in question
    2. Convert it to a Number
      1. Convert it to a primitive
        1. Call the internal [[DefaultValue]] method

提交回复
热议问题