代码之家  ›  专栏  ›  技术社区  ›  Ljiljana Matic

优化半小时间隔阵列中的排序时间

  •  1
  • Ljiljana Matic  · 技术社区  · 8 年前

    我开发了一个小函数,可以对具有 上次访问时间 将日期时间信息放入sortedData数组中,该数组包含相同的信息,但每隔半小时放置一次(preparedData是一个数组,仅包含开始和结束时间间隔信息)。

    我的问题是:这是实现预期结果的最佳方式吗?随着数据数组变大,prepareData()变慢,因此我需要尽量减少准备时间。 创建了一个小的检查逻辑 https://jsfiddle.net/mtcljiljana/nvfq6h9h/

    const data = [{"lastAccessedTime":"2017-11-16T07:15:41Z","path":"/login.htm"},{"lastAccessedTime":"2017-11-16T07:32:42Z","path":"/login.htm"},{"lastAccessedTime":"2017-11-16T07:32:47Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:36:02Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:36:55Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:37:28Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:39:48Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:40:47Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:42:31Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:44:06Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:46:04Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:46:43Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:50:31Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:52:54Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T07:53:53Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T08:14:00Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T08:14:44Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T08:16:20Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T08:17:32Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T08:19:21Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T08:20:13Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T08:20:59Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T08:22:59Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T08:25:14Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T08:26:17Z","path":"/concurrent_users.htm"},{"id":913,"cookieId":"WfP2r2jiq4xAKo9YziaV5winTllwY5HL","creationTime":"2017-11-16T08:47:15Z","lastAccessedTime":"2017-11-16T08:47:15Z","path":"/login.htm"},{"lastAccessedTime":"2017-11-16T08:49:47Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:12:39Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:24:25Z","path":"/login.htm"},{"lastAccessedTime":"2017-11-16T09:26:15Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:28:10Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:28:53Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:30:49Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:31:48Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:34:01Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:43:47Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:44:21Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:44:30Z","path":"/login.htm"},{"lastAccessedTime":"2017-11-16T09:44:34Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:44:48Z","path":"/"},{"lastAccessedTime":"2017-11-16T09:44:54Z","path":"/login.htm"},{"lastAccessedTime":"2017-11-16T09:44:56Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:45:09Z","path":"/"},{"lastAccessedTime":"2017-11-16T09:45:14Z","path":"/login.htm"},{"lastAccessedTime":"2017-11-16T09:45:16Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:45:30Z","path":"/"},{"lastAccessedTime":"2017-11-16T09:45:35Z","path":"/login.htm"},{"lastAccessedTime":"2017-11-16T09:45:38Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:49:31Z","path":"/settings/license.htm"},{"lastAccessedTime":"2017-11-16T09:50:47Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:50:57Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:51:32Z","path":"/settings/license.htm"},{"lastAccessedTime":"2017-11-16T09:52:06Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:58:55Z","path":"/concurrent_users.htm"},{"lastAccessedTime":"2017-11-16T09:59:12Z","path":"/connections/qc.htm"},{"lastAccessedTime":"2017-11-16T09:59:22Z","path":"/connections/rally.htm"},{"lastAccessedTime":"2017-11-16T10:07:10Z","path":"/connections/rally.htm"},{"lastAccessedTime":"2017-11-16T10:11:21Z","path":"/connections/rally.htm"},{"lastAccessedTime":"2017-11-16T10:22:48Z","path":"/connections/rally.htm"},{"lastAccessedTime":"2017-11-16T10:30:27Z","path":"/connections/rally.htm"},{"lastAccessedTime":"2017-11-16T10:34:10Z","path":"/connections/rally.htm"}];
    
    const prepareData = (data) => {
      const sortedData = [];
      const startDate = [];
      const endDate = [];
      // transform GMT to local time
      data.forEach(d => {
        d.lastAccessedTime = moment(d.lastAccessedTime).format('YYYY-MM-DD HH:mm:ss');
      });
      // sort dates in ascending order
      data.sort((a, b) => moment(a.lastAccessedTime) - moment(b.lastAccessedTime));
    
      data.forEach(d => {
        const start = moment(d.lastAccessedTime);
        let put = sortedData;
        let endMinutes;
        let startMinutes;
    
        if (start.minute() < 30) {
          endMinutes = 30 - start.minute();
          startMinutes = -start.minute();
        } else {
          endMinutes = 60 - start.minute();
          startMinutes = 30 - start.minute();
        };
    
        const startTime = moment(start)
          .add(startMinutes, 'minutes')
          .add(-start.seconds(), 'seconds')
          .format('YYYY-MM-DD HH:mm:ss');
    
        const endTime = moment(start)
          .add(endMinutes, 'minutes')
          .add(-start.seconds(), 'seconds')
          .format('YYYY-MM-DD HH:mm:ss');
    
        sortedData.forEach(itm2 => {
          itm2.forEach(itm3 => {
            const date = moment(itm3.lastAccessedTime);
            const min = moment(startTime);
            const max = moment(endTime);
            if (date < max && date > min) {
              put = itm2;
            }
          });
        });
    
        if (put === sortedData) {
          startDate.push(startTime);
          endDate.push(endTime);
          put.push([d]);
        } else {
          put.push(d);
        };
      });
    
      const preparedData = sortedData.map((data, i) => {
        const object = {
          startDate: startDate[i],
          endDate: endDate[i]
        };
        return object;
      });
      console.log(JSON.stringify(sortedData));
      console.log(JSON.stringify(preparedData));
    };
    prepareData(data);
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Nina Scholz    8 年前

    可以使用 ISO 8601 日期字符串,并采用哈希表对同一时隙进行分组。

    var array = [{ lastAccessedTime: "2017-11-16T07:15:41Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T07:32:42Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T07:32:47Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:36:02Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:36:55Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:37:28Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:39:48Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:40:47Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:42:31Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:44:06Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:46:04Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:46:43Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:50:31Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:52:54Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:53:53Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:14:00Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:14:44Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:16:20Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:17:32Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:19:21Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:20:13Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:20:59Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:22:59Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:25:14Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:26:17Z", path: "/concurrent_users.htm" }, { id: 913, cookieId: "WfP2r2jiq4xAKo9YziaV5winTllwY5HL", creationTime: "2017-11-16T08:47:15Z", lastAccessedTime: "2017-11-16T08:47:15Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T08:49:47Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:12:39Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:24:25Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T09:26:15Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:28:10Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:28:53Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:30:49Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:31:48Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:34:01Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:43:47Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:44:21Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:44:30Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T09:44:34Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:44:48Z", path: "/" }, { lastAccessedTime: "2017-11-16T09:44:54Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T09:44:56Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:45:09Z", path: "/" }, { lastAccessedTime: "2017-11-16T09:45:14Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T09:45:16Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:45:30Z", path: "/" }, { lastAccessedTime: "2017-11-16T09:45:35Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T09:45:38Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:49:31Z", path: "/settings/license.htm" }, { lastAccessedTime: "2017-11-16T09:50:47Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:50:57Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:51:32Z", path: "/settings/license.htm" }, { lastAccessedTime: "2017-11-16T09:52:06Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:58:55Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:59:12Z", path: "/connections/qc.htm" }, { lastAccessedTime: "2017-11-16T09:59:22Z", path: "/connections/rally.htm" }, { lastAccessedTime: "2017-11-16T10:07:10Z", path: "/connections/rally.htm" }, { lastAccessedTime: "2017-11-16T10:11:21Z", path: "/connections/rally.htm" }, { lastAccessedTime: "2017-11-16T10:22:48Z", path: "/connections/rally.htm" }, { lastAccessedTime: "2017-11-16T10:30:27Z", path: "/connections/rally.htm" }, { lastAccessedTime: "2017-11-16T10:34:10Z", path: "/connections/rally.htm" }],
        groups = Object.create(null),
        result = [];
    
    array.sort((a, b) => a.lastAccessedTime > b.lastAccessedTime || -(a.lastAccessedTime < b.lastAccessedTime));
    
    array.forEach(function (o) {
        var slot = Math.floor(o.lastAccessedTime.slice(14, 16) / 30),
            key = o.lastAccessedTime.slice(0, 14) + (slot ? '30' : '00');
        if (!groups[key]) {
            groups[key] = [];
            result.push(groups[key]);
        }
        groups[key].push(o);
    });
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
        2
  •  0
  •   Daphoque    8 年前

    优化方法:

    1/尝试删除矩,改用日期函数。

    2/尝试移除/更改回路内部回路:

     data.forEach(d => {
        ....
        sortedData.forEach(itm2 => {
           itm2.forEach(itm3 => {
               ....
           });
        });
     });