Tests:
  • Map

    x
     
    const createLists = (arr = []) => Array.from(
      arr.reduce((map, val) => {
        if (!map.has(val)) map.set(val, []);
        map.get(val).push(val);
        return map;
      }, new Map()).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: one year ago)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36
Chrome 118 on Linux
View result in a separate tab
Test name Executions per second
Map 23956.1 Ops/sec
forEach 20761.6 Ops/sec