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.
This commit is contained in:
Sam Saffron 2020-03-26 17:36:42 +11:00
parent 25f1f23288
commit e62071830d
No known key found for this signature in database
GPG Key ID: B9606168D2FFD9F5
10 changed files with 16 additions and 16 deletions

View File

@ -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);
}
}
}

View File

@ -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);
}
},

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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)) {

View File

@ -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() });
}
}
});