Tests:
  • Map

    x
     
    const createLists = (arr = []) => {
      const listMap = new Map();
      for (const val of arr) {
        if (!listMap.has(val)) {
          listMap.set(val, [val]);
          continue;
        } 
        listMap.get(val).push(val);
      }
      return Array.from(listMap.values());
    };
    console.log(createLists([1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 0, 0, 0, 0, 0]));
  • forEach

     
    function createLists(arr = []) {
      let authors = [];
      let list = [];
      arr.forEach(item => {
        let authorIndex = authors.includes(item) ? authors.indexOf(item) : (authors.push(item) - 1);
        list[authorIndex] = list[authorIndex] || [];
        list[authorIndex].push(item);
      });
      return list;
    }
    console.log(createLists([1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 0, 0, 0, 0, 0]));
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Map
    forEach

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 3 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0
Firefox 86 on Windows
View result in a separate tab
Test name Executions per second
Map 32319.3 Ops/sec
forEach 47775.2 Ops/sec