Sort with no change on matching vs Push lower values to back

Benchmark created on


Description

This shows the difference performance wise over sorting two lists.

  • One sort where we don't move items that are matching values.
  • One where we only move higher values to the front of the array, other values get pushed regardless

    Conclusion: Even though Array.sort sorts "in-place", it's logically quicker to tell the sort method to do nothing if it's the same value

Setup

const val = [6, 3, 4, 7, 1, 2, 3, 6]

Test runner

Ready to run.

Testing in
TestOps/sec
Sort with no change on matching
val.sort((a, b) => {
    if (a > b) {
      return 1;
    }
    
    if (a < b) {
    	return -1
    }

    return 0;
})
ready
Push lower values to back
val.sort((a, b) => {
    if (a > b) {
      return 1;
    }

    return -1;
})
ready

Revisions

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