sort vs toSorted (v5)

Revision 5 of this benchmark created on


Preparation HTML

<script>
function generateTasks(numTasks ) {
  const priorities = ['high', 'medium', 'low'];
  const tasks= [];

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

  return tasks;
}

// Generate a large dataset
const largeTaskDataset = generateTasks(10000);
</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.