DEV: prefer Date.now() over new Date().getTime()

DEV: prefer Date.now() over new Date().getTime()

New Date().getTime() is both uglier and slower than Date.now()

Date.now() is available on all the browsers we support.

diff --git a/app/assets/javascripts/discourse/components/mount-widget.js b/app/assets/javascripts/discourse/components/mount-widget.js
index dc98bf9..dc1eb2e 100644
--- a/app/assets/javascripts/discourse/components/mount-widget.js
+++ b/app/assets/javascripts/discourse/components/mount-widget.js
@@ -112,7 +112,7 @@ export default Component.extend({
         return;
       }
 
-      const t0 = new Date().getTime();
+      const t0 = Date.now();
       const args = this.args || this.buildArgs();
       const opts = {
         model: this.model,
@@ -139,7 +139,7 @@ export default Component.extend({
 
       if (this.profileWidget) {
         // eslint-disable-next-line no-console
-        console.log(new Date().getTime() - t0);
+        console.log(Date.now() - t0);
       }
     }
   }
diff --git a/app/assets/javascripts/discourse/components/signup-cta.js b/app/assets/javascripts/discourse/components/signup-cta.js
index 7d6f162..43aee26 100644
--- a/app/assets/javascripts/discourse/components/signup-cta.js
+++ b/app/assets/javascripts/discourse/components/signup-cta.js
@@ -12,7 +12,7 @@ export default Component.extend({
     },
     hideForSession() {
       this.session.set("hideSignupCta", true);
-      this.keyValueStore.setItem("anon-cta-hidden", new Date().getTime());
+      this.keyValueStore.setItem("anon-cta-hidden", Date.now());
       later(() => this.session.set("showSignupCta", false), 20 * 1000);
     }
   },
diff --git a/app/assets/javascripts/discourse/initializers/signup-cta.js b/app/assets/javascripts/discourse/initializers/signup-cta.js
index a46d12f..db674cc 100644
--- a/app/assets/javascripts/discourse/initializers/signup-cta.js
+++ b/app/assets/javascripts/discourse/initializers/signup-cta.js
@@ -38,7 +38,7 @@ export default {
         return; // hidden forever
       }
 
-      const now = new Date().getTime();
+      const now = Date.now();
       const hiddenAt = keyValueStore.getInt("anon-cta-hidden", 0);
       if (hiddenAt > now - PROMPT_HIDE_DURATION) {
         return; // hidden in last 24 hours
diff --git a/app/assets/javascripts/discourse/lib/lock-on.js b/app/assets/javascripts/discourse/lib/lock-on.js
index 90cc641..d17353d 100644
--- a/app/assets/javascripts/discourse/lib/lock-on.js
+++ b/app/assets/javascripts/discourse/lib/lock-on.js
@@ -49,7 +49,7 @@ export default class LockOn {
   }
 
   lock() {
-    const startedAt = new Date().getTime();
+    const startedAt = Date.now();
     let previousTop = this.elementTop();
     previousTop && $(window).scrollTop(previousTop);
 
@@ -73,7 +73,7 @@ export default class LockOn {
       }
 
       // Stop after a little while
-      if (new Date().getTime() - startedAt > LOCK_DURATION_MS) {
+      if (Date.now() - startedAt > LOCK_DURATION_MS) {
         return this.clearLock(interval);
       }
     }, 50);
diff --git a/app/assets/javascripts/discourse/lib/screen-track.js b/app/assets/javascripts/discourse/lib/screen-track.js
index 91e0d42..ccfd9d8 100644
--- a/app/assets/javascripts/discourse/lib/screen-track.js
+++ b/app/assets/javascripts/discourse/lib/screen-track.js
@@ -65,7 +65,7 @@ export default class {
 
   // Reset our timers
   reset() {
-    const now = new Date().getTime();
+    const now = Date.now();
     this._lastTick = now;
     this._lastScrolled = now;
     this._lastFlush = 0;
@@ -79,7 +79,7 @@ export default class {
   }
 
   scrolled() {
-    this._lastScrolled = new Date().getTime();
+    this._lastScrolled = Date.now();
   }
 
   registerAnonCallback(cb) {
@@ -212,7 +212,7 @@ export default class {
   }
 
   tick() {
-    const now = new Date().getTime();
+    const now = Date.now();
 
     // If the user hasn't scrolled the browser in a long time, stop tracking time read
     const sinceScrolled = now - this._lastScrolled;
diff --git a/app/assets/javascripts/discourse/lib/url.js b/app/assets/javascripts/discourse/lib/url.js
index a475c4a..ed46452 100644
--- a/app/assets/javascripts/discourse/lib/url.js
+++ b/app/assets/javascripts/discourse/lib/url.js
@@ -368,7 +368,7 @@ const DiscourseURL = EmberObject.extend({
           const closest = postStream.closestPostNumberFor(opts.nearPost || 1);
           topicController.setProperties({
             "model.currentPost": closest,
-            enteredAt: new Date().getTime().toString()
+            enteredAt: Date.now().toString()
           });
 
           this.appEvents.trigger("post:highlight", closest);
diff --git a/app/assets/javascripts/discourse/mixins/pan-events.js b/app/assets/javascripts/discourse/mixins/pan-events.js
index fb05901..b41d002 100644
--- a/app/assets/javascripts/discourse/mixins/pan-events.js
+++ b/app/assets/javascripts/discourse/mixins/pan-events.js
@@ -61,7 +61,7 @@ export default Mixin.create({
     if (e.type === "pointerup" || e.type === "pointercancel") {
       return oldState;
     }
-    const newTimestamp = new Date().getTime();
+    const newTimestamp = Date.now();
     const timeDiffSeconds = newTimestamp - oldState.timestamp;
     if (timeDiffSeconds === 0) {
       return oldState;
@@ -109,7 +109,7 @@ export default Mixin.create({
       deltaY: 0,
       distance: 0,
       start: true,
-      timestamp: new Date().getTime(),
+      timestamp: Date.now(),
       direction: null
     };
     this.set("_panState", newState);
diff --git a/app/assets/javascripts/discourse/routes/topic-from-params.js b/app/assets/javascripts/discourse/routes/topic-from-params.js
index dd22155..1429fb0 100644
--- a/app/assets/javascripts/discourse/routes/topic-from-params.js
+++ b/app/assets/javascripts/discourse/routes/topic-from-params.js
@@ -53,7 +53,7 @@ export default DiscourseRoute.extend({
         topicController.setProperties({
           "model.currentPost": closest,
           enteredIndex: topic.postStream.progressIndexOfPost(closestPost),
-          enteredAt: new Date().getTime().toString()
+          enteredAt: Date.now().toString()
         });
 
         this.appEvents.trigger("page:topic-loaded", topic);
diff --git a/app/assets/javascripts/discourse/widgets/post-edits-indicator.js b/app/assets/javascripts/discourse/widgets/post-edits-indicator.js
index 46f7ed3..f928df4 100644
--- a/app/assets/javascripts/discourse/widgets/post-edits-indicator.js
+++ b/app/assets/javascripts/discourse/widgets/post-edits-indicator.js
@@ -13,7 +13,7 @@ export function historyHeat(siteSettings, updatedAt) {
   }
 
   // Show heat on age
-  const rightNow = new Date().getTime();
+  const rightNow = Date.now();
   const updatedAtTime = updatedAt.getTime();
 
   if (updatedAtTime > rightNow - mult(siteSettings.history_hours_low)) {
diff --git a/app/assets/javascripts/discourse/widgets/post.js b/app/assets/javascripts/discourse/widgets/post.js
index c6fc4f3..d2f7933 100644
--- a/app/assets/javascripts/discourse/widgets/post.js
+++ b/app/assets/javascripts/discourse/widgets/post.js
@@ -730,7 +730,7 @@ export default createWidget("post", {
     const lastWarnedLikes = kvs.get("lastWarnedLikes");
 
     // only warn once per day
-    const yesterday = new Date().getTime() - 1000 * 60 * 60 * 24;
+    const yesterday = Date.now() - 1000 * 60 * 60 * 24;
     if (lastWarnedLikes && parseInt(lastWarnedLikes, 10) > yesterday) {
       return;
     }
@@ -739,7 +739,7 @@ export default createWidget("post", {
     const threshold = Math.ceil(max * 0.1);
     if (remaining === threshold) {
       bootbox.alert(I18n.t("post.few_likes_left"));
-      kvs.set({ key: "lastWarnedLikes", value: new Date().getTime() });
+      kvs.set({ key: "lastWarnedLikes", value: Date.now() });
     }
   }
 });

GitHub sha: e6207183

1 Like