HTML Preparation code:
AخA
 
1
<!--your preparation HTML code goes here-->
Tests:
  • transform1

    x
     
    var transform1 = (rowTemplate) => {
        if (!rowTemplate?.length) {
            return [[]];
        }
        const rows = rowTemplate.length;
        const cols = rowTemplate[0].length;
        const tiles = new Map();
        for (let row = 0; row < rows; row++) {
            for (let col = 0; col < cols; col++) {
                const id = rowTemplate[row][col];
                if (!tiles.has(id)) {
                    tiles.set(id, { positions: [] });
                }
                tiles.get(id)?.positions.push({ row, col });
            }
        }
        const tilesByColumn = Array.from(tiles.keys()).sort((idA, idB) => {
            const posA = tiles.get(idA)?.positions[0];
            const posB = tiles.get(idB)?.positions[0];
            return posA.col === posB.col
                ? posA.row - posB.row
                : posA.col - posB.col;
        });
        const tilesByRow = Array.from(tiles.keys()).sort((idA, idB) => {
            const posA = tiles.get(idA)?.positions[0];
            const posB = tiles.get(idB)?.positions[0];
            return posA.row === posB.row
                ? posA.col - posB.col
                : posA.row - posB.row;
        });
        const tileMap = {};
        tilesByRow.forEach((rowId, index) => {
            tileMap[rowId] = tilesByColumn[index];
        });
        const result = Array(rows).fill(null)
            .map(() => Array(cols).fill(null));
        for (let row = 0; row < rows; row++) {
            for (let col = 0; col < cols; col++) {
                const originalId = rowTemplate[row][col];
                result[row][col] = tileMap[originalId];
            }
        }
        return result;
    };
    var t1 = [
        ["1", "1", "2", "3", "4", "4"],
        ["1", "1", "5", "6", "4", "4"]
    ];
    for (var i=0; i < 10000; i++) {
        transform1(t1);
    }
  • transform2

     
    var transform2 = (rowTemplate) => {
      if (!rowTemplate?.length) return [[]];
      const rows = rowTemplate.length;
      const cols = rowTemplate[0].length;
      
      const firstPositions = {};
      
      for (let row = 0; row < rows; row++) {
        for (let col = 0; col < cols; col++) {
          const id = rowTemplate[row][col];
          if (firstPositions[id] === undefined) {
            firstPositions[id] = {row, col};
          }
        }
      }
      
      const uniqueIds = Object.keys(firstPositions);
      
      const rowIds = [...uniqueIds].sort((a, b) => {
        const posA = firstPositions[a];
        const posB = firstPositions[b];
        return posA.row === posB.row ? posA.col - posB.col : posA.row - posB.row;
      });
      
      const colIds = [...uniqueIds].sort((a, b) => {
        const posA = firstPositions[a];
        const posB = firstPositions[b];
        return posA.col === posB.col ? posA.row - posB.row : posA.col - posB.col;
      });
      
      const tileMap = {};
      
      for (let i = 0; i < rowIds.length; i++) {
        tileMap[rowIds[i]] = colIds[i];
      }
      
      const result = Array(rows);
      for (let row = 0; row < rows; row++) {
        result[row] = new Array(cols);
        for (let col = 0; col < cols; col++) {
          result[row][col] = tileMap[rowTemplate[row][col]];
        }
      }
      
      return result;
    };
    var t1 = [
        ["1", "1", "2", "3", "4", "4"],
        ["1", "1", "5", "6", "4", "4"]
    ];
    for (var i=0; i < 10000; i++) {
        transform2(t1);
    }
  • transform3

     
    const transform3 = (rowTemplate) => {
        if (!rowTemplate?.length) {
            return [];
        }
        const rows = rowTemplate.length;
        const cols = rowTemplate[0].length;
        const result = [];
        for (let col = 0; col < cols; col++) {
            for (let row = 0; row < rows; row++) {
                result.push(rowTemplate[row][col]);
            }
        }
        return result;
    };
    var t1 = [
        ["1", "1", "2", "3", "4", "4"],
        ["1", "1", "5", "6", "4", "4"]
    ];
    for (var i=0; i < 10000; i++) {
        transform3(t1);
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    transform1
    transform2
    transform3

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 27 days ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Chrome 133 on Windows
View result in a separate tab
Test name Executions per second
transform1 72.4 Ops/sec
transform2 98.8 Ops/sec
transform3 3693.3 Ops/sec