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

时间:2017-11-20 13:29:12

标签: javascript sorting optimization momentjs

我开发了一个小函数,它将对具有的对象数组进行排序 lastAccessedTime datetime信息到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 个答案:

答案 0 :(得分:1)

您可以使用ISO 8601日期字符串对数组进行排序,并使用哈希表对同一时段进行分组。

&#13;
&#13;
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

优化方式:

1 /尝试删除时刻并改为使用日期功能。

2 /尝试删除/更改循环内循环内部的循环:

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