Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Is sourcemap mappings sorted? (https://jsbench.github.io/#b5130ae558bbca6b002025aca004201e) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Is sourcemap mappings sorted?</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 () {
const mappings = [];
for (let i = 0; i < 100; i++) {
const line = [];
for (let j = 0; j < 1000; j++) {
line.push([i]);
}
mappings.push(line);
}
function binarySearch(haystack, needle, comparator) {
let low = 0;
let high = haystack.length - 1;
while (low <= high) {
const mid = low + ((high - low) >> 1);
const cmp = comparator(haystack[mid], needle);
if (cmp === 0) {
return mid;
}
if (cmp < 0) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return ~low;
}
function segmentComparator(segment, column) {
return segment[0] - column;
}
function sortSegments(segments) {
segments.sort(segmentComparator);
}
function segmentComparator(a, b) {
return a[0] - b[0];
}
};
suite.add("checking uf map is sorted", function () {
// checking uf map is sorted
let sorted = true;
for (let i = 0; i < mappings.length; i++) {
const line = mappings[i];
for (let j = 1; j < line.length; j++) {
if (line[j - 1][0] > line[j][0]) {
sorted = true;
}
}
}
});
suite.add("tracing 1/8th of the map", function () {
// tracing 1/8th of the map
for (let i = 0; i < 25; i++) {
const line = mappings[i];
for (let j = 0; j < 250; j++) {
binarySearch(line, j, segmentComparator)
}
}
});
suite.add("Sorting the map", function () {
// Sorting the map
for (let i = 0; i < mappings.length; i++) {
const line = mappings[i];
sortSegments(line)
}
});
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("Is sourcemap mappings sorted?");
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