Skip to content

Instantly share code, notes, and snippets.

@jridgewell
Created March 18, 2021 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jridgewell/f32d70e88170ab4e165ac1f7f30df163 to your computer and use it in GitHub Desktop.
Save jridgewell/f32d70e88170ab4e165ac1f7f30df163 to your computer and use it in GitHub Desktop.
Stable sorting #jsbench #jsperf (https://jsbench.github.io/#f32d70e88170ab4e165ac1f7f30df163) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Stable sorting #jsbench #jsperf</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
function makeArray(n) {
let array = [];
for (let i = 0; i < n; i++) {
array[i] = { p: (Math.random() * 100) | 0 };
}
return array.slice();
}
function priority(obj) {
return obj.p % 5;
}
function sorter(a, b) {
return priority(a) - priority(b);
}
function stableSort(array, index) {
// By default, we use priorities of 0-4.
const buckets = [[], [], [], [], []];
// By collecting into buckets, we can guarantee a stable sort.
for (let i = index; i < array.length; i++) {
const n = array[i];
const p = priority(n);
// In case some plugin is setting an unexpected priority.
(buckets[p] || (buckets[p] = [])).push(n);
}
// Highest priorities go first
for (let i = buckets.length - 1; i >= 0; i--) {
const bucket = buckets[i];
// In that unexpected priority caused a sparse array.
if (!bucket) continue;
for (const n of bucket) {
array[index++] = n;
}
}
}
};
suite.add("Native sort, small array", function () {
// Native sort, small array
const array = makeArray(10);
array.sort(sorter);
});
suite.add("stableSort, small array", function () {
// stableSort, small array
const array = makeArray(10);
stableSort(sorter, 0);
});
suite.add("Native sort, large array", function () {
// Native sort, large array
const array = makeArray(100);
array.sort(sorter);
});
suite.add("stableSort, large array", function () {
// stableSort, large array
const array = makeArray(100);
stableSort(sorter, 0);
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("Stable sorting #jsbench #jsperf");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment