From 629594ece5ed2229143ca1f1b62c07aa0824b35e Mon Sep 17 00:00:00 2001 From: Thijs Triemstra Date: Wed, 24 Apr 2019 17:29:22 +0200 Subject: [PATCH] fix: use performance.now() when possible (#5870) --- src/js/component.js | 4 ++-- src/js/utils/dom-data.js | 3 ++- src/js/utils/fn.js | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/js/component.js b/src/js/component.js index 9b317ab44..b9fd11272 100644 --- a/src/js/component.js +++ b/src/js/component.js @@ -1128,7 +1128,7 @@ class Component { pageY: event.touches[0].pageY }; // Record start time so we can detect a tap vs. "touch and hold" - touchStart = new Date().getTime(); + touchStart = window.performance.now(); // Reset couldBeTap tracking couldBeTap = true; } @@ -1166,7 +1166,7 @@ class Component { // Proceed only if the touchmove/leave/cancel event didn't happen if (couldBeTap === true) { // Measure how long the touch lasted - const touchTime = new Date().getTime() - touchStart; + const touchTime = window.performance.now() - touchStart; // Make sure the touch was less than the threshold to be considered a tap if (touchTime < touchTimeThreshold) { diff --git a/src/js/utils/dom-data.js b/src/js/utils/dom-data.js index 71ad4180f..de39dae60 100644 --- a/src/js/utils/dom-data.js +++ b/src/js/utils/dom-data.js @@ -3,6 +3,7 @@ * @module dom-data */ import * as Guid from './guid.js'; +import window from 'global/window'; /** * Element Data Store. @@ -23,7 +24,7 @@ export const elData = {}; * @constant * @private */ -const elIdAttr = 'vdata' + (new Date()).getTime(); +const elIdAttr = 'vdata' + Math.floor(window.performance && window.performance.now() || Date.now()); /** * Returns the cache object where data for an element is stored diff --git a/src/js/utils/fn.js b/src/js/utils/fn.js index 151958feb..4f6b0d6b1 100644 --- a/src/js/utils/fn.js +++ b/src/js/utils/fn.js @@ -61,10 +61,10 @@ export const bind = function(context, fn, uid) { * @return {Function} */ export const throttle = function(fn, wait) { - let last = Date.now(); + let last = window.performance.now(); const throttled = function(...args) { - const now = Date.now(); + const now = window.performance.now(); if (now - last >= wait) { fn(...args);