Tests:
  • map

    x
     
    const cuteObject = {
      [1]: [ { name: 'luna', id: 135 } ],
      [3]: [ { name: 'fat', id: 136 } ]
    };
    const newList = Object.keys(cuteObject).map(key => {
        const options = cuteObject[key].map(item => ({
          label: item.name,
          name: item.name,
          id: item.id,
        }));
        return {
          label: key,
          options: options,
        };
      });
  • forEach

     
    const cuteObject = {
      [1]: [ { name: 'luna', id: 135 } ],
      [3]: [ { name: 'fat', id: 136 } ]
    };
    const newList = [];
    Object.keys(cuteObject).forEach(key => {
        const options = [];
        cuteObject[key].forEach(item => {
            options.push({
                label: item.name,
                name: item.name,
                id: item.id,
            });
        });
        newList.push({
            label: key,
            options: options,
        });
    });
  • for loop

     
    const cuteObject = {
      [1]: [ { name: 'luna', id: 135 } ],
      [3]: [ { name: 'fat', id: 136 } ]
    };
    const newList = [];
    const keys = Object.keys(cuteObject);
    const keysLength = keys.length;
    for (let i = 0; i < keysLength; i++) {
        const key = keys[i];
        const options = [];
        const items = cuteObject[key];
        const itemsLength = items.length;
        for (let j = 0; j < itemsLength; j++) {
            const item = items[j];
            options.push({
                label: item.name,
                name: item.name,
                id: item.id,
            });
        }
        newList.push({
            label: key,
            options: options,
        });
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    map
    forEach
    for loop

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Chrome 120 on Windows
View result in a separate tab
Test name Executions per second
map 1725395.5 Ops/sec
forEach 1679411.2 Ops/sec
for loop 1737686.5 Ops/sec