2017-11-20 6 views
1

を時間ソート最適化(preparedDataであります配列は、開始および終了時間間隔情報のみを保持する)。はIを同じ情報を保持sortedData配列に <strong>lastAccessedTime</strong>日時情報を持つオブジェクトの配列をソートします小さな機能を開発したが、半分の時間間隔で置かれた半時間間隔のアレイに

私の質問です:期待した結果を達成するための最良の方法ですか?データ配列が大きくなるにつれ、prepareData()は遅くなるので、準備をする時間を最小限に抑える必要があります。

1 /瞬間を削除し、代わりに日付関数を使用してみてください: はロジックに最適化する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); 
+0

指名手配結果の小さな例を追加してください。 –

+0

現在の結果は必要な結果です。より少ないコード行でより速く同じ結果を達成しようとしています –

+0

あなたは2つのconsole.log出力を見ることができます –

答えて

1

あなたは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; }

0

ウェイをチェックアウトする小さなフィドルを作成しました。

2 /ループ内でループ内のループを変更/削除するようにしてください:

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

削除の瞬間はオプションではありません。私は 'loop inside loop 'ループに満足していませんが、私が試したことがないかどうかは私には同じ結果を与えません。 –

+0

私はちょうど瞬間を削除することで14の速い結果を得ます(10000回のループに対して10msと140ms、https://pastebin.com/puWhbnQgとhttps://pastebin.com/Pez5zhWrを参照)。しかし、主な問題はループの埋め込みです – Dafuck

関連する問題