简体   繁体   English

使用 localStorage / sessionStorage 的性能问题?

[英]Performance issues with using localStorage / sessionStorage?

I am trying to detect user inactivity for a certain amount of time and based on it need to perform certain actions.我试图检测用户在一段时间内不活动,并根据它需要执行某些操作。

I am using localStorage for this so that i set the idle time start across all tabs open.我为此使用 localStorage,以便我在所有打开的选项卡中设置空闲时间开始。

below is the relevant part of my code下面是我的代码的相关部分

  const detect = () => {
    //console.log(`time is ${idleStartTime}`);
    if(Date.now() - getIdleStartTime() > ONE_HOUR){
      console.log('idle time more than one hour....');
      console.log(`${getIdleStartTime()} ended`);
      alert('idle time more than one hour....');
    } else {
      idleAnimationFrameId = requestAnimationFrame(function() {
        detect();
      });
    }
  };

  const startTimer = () => {
    idleAnimationFrameId = requestAnimationFrame(function() {
      detect();
    });
  };

  function setIdleStartTime() {
    return new Promise((resolve, reject) => {
      if (storageAvailable('localStorage')) {
        // Yippee!
        localStorage.setItem('idleStartTime', Date.now());
        console.log('idle time has been set...');
        resolve(true);
      }
      else {
        // Too bad, no localStorage for us
        console.log('no local storage support...');
        reject('no local storage support.')
      }
    });
  }

I mark the user as active if by listing to the following events.如果通过列出以下事件,我将用户标记为活动。 ['mousedown', 'mousemove', 'keydown', 'scroll', 'touchstart'];

  function setEventListners() {
    activityEvents.forEach(function(eventName) {
      document.addEventListener(eventName, activity, true);
    });
  }

  function activity() {
    console.log('user activity detected...');
    localStorage.setItem('idleStartTime', Date.now());
  }

I see that in some cases there are 1.5K request sent in a very short time to localstorage to set the value.我看到在某些情况下,有 1.5K 请求在很短的时间内发送到 localstorage 以设置该值。 I see user activity detected... printed on the console around 1.5k times in matter of seconds.我看到user activity detected...在几秒钟内在控制台上打印了大约 1.5k 次。

My couple of questions are我的几个问题是

  1. will there be any performance issues as i am setting the value idleStartTime in localStorage and local storage set will be called thousands of time in a few seconds.当我在 localStorage 中设置idleStartTime值时会出现任何性能问题,并且本地存储集将在几秒钟内被调用数千次。

  2. Are there any better alertnatives than using localStorage for my scenario.有没有比在我的场景中使用 localStorage 更好的警报器。

Thanks.谢谢。

Answer to Question-1问题 1 的回答

There will be no memory limit issue since every time you call localStorage.setItem , you are overwriting on the previous value.不会有内存限制问题,因为每次调用localStorage.setItem ,都会覆盖之前的值。 But you mentioned that your activity change is firing 1.5K times in a very short amount of time.但是您提到您的活动更改在很短的时间内触发了 1.5K 次。 That will increase disk usage(I/O) time.这将增加磁盘使用(I/O)时间。

Answer to Question-2问题 2 的回答

  1. You can use setTimeout instead of event listeners.您可以使用 setTimeout 代替事件侦听器。 Many website use setTimeout 1 or 2 minutes to detect idle time.许多网站使用 setTimeout 1 或 2 分钟来检测空闲时间。 The logic is to check whether all your input fields are same as 2 minutes ago.逻辑是检查您的所有输入字段是否与 2 分钟前相同。 If they are same then it is considered that the page is in idle mode for that amount of time.如果它们相同,则认为该页面在这段时间内处于空闲模式。 I know this approach seems ancient, but it will significantly reduce I/O time.我知道这种方法看起来很古老,但它会显着减少 I/O 时间。
  2. You can skip cursor move event.您可以跳过光标移动事件。 That alone will reduce the checking in a huge amount.仅此一项就可以大量减少检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM