sort vs toSorted (v6)

Revision 6 of this benchmark created on


Preparation HTML

<script>
function generateTasks(numTasks ) {
  const tasks= [];

  for (let i = 0; i < numTasks; i++) {
    tasks.push({
      id: i + 1,
      name: `Task ${i + 1}`,
      priority: Math.floor(Math.random()*100),
    });
  }

  return tasks;
}

// Generate a large dataset
const largeTaskDataset = generateTasks(100000);
</script>

Setup


Test runner

Ready to run.

Testing in
TestOps/sec
.sort
// Test Case 1: Bubble Sort
function nativeSort(tasks) {
  return tasks.sort((a, b) => a.priority > b.priority)
}
const sortedTasks = nativeSort([...largeTaskDataset]);
ready
.toSorted
function nativeToSorted(tasks) {
  return tasks.toSorted((a, b) => a.priority > b.priority)
}
const quickSortResult = nativeToSorted([...largeTaskDataset]);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.