Skip to content

Instantly share code, notes, and snippets.

@jfirebaugh
Forked from mourner/bench_geodata.js
Created April 27, 2018 13:08
Show Gist options
  • Save jfirebaugh/e1a5fe6967d82f99db52e47b1dc8c5c4 to your computer and use it in GitHub Desktop.
Save jfirebaugh/e1a5fe6967d82f99db52e47b1dc8c5c4 to your computer and use it in GitHub Desktop.
const numFeatures = 100;
const numPolygons = 5;
const numRings = 10;
const numPoints = 10000;
console.time('populate storage');
const features = [];
for (let i = 0; i < numFeatures; i++) {
const feature = [];
features.push(feature);
for (let j = 0; j < numPolygons; j++) {
const polygon = [];
feature.push(polygon);
for (let k = 0; k < numRings; k++) {
const ring = [];
polygon.push(ring);
for (let m = 0; m < numPoints; m++) {
ring.push(Math.floor(Math.random() * 4096));
ring.push(Math.floor(Math.random() * 4096));
}
}
}
}
console.timeEnd('populate storage');
function forEachPoint(ring, fn) {
for (let i = 0; i < ring.length; i += 2) {
fn(ring[i], ring[i + 1]);
}
}
console.time('flat bbox');
{
let minX = Infinity;
let minY = Infinity;
let maxX = Infinity;
let maxY = Infinity;
for (const feature of features) {
for (const polygon of feature) {
for (const ring of polygon) {
for (let i = 0; i < ring.length; i += 2) {
const x = ring[i];
const y = ring[i + 1];
if (x < minX) minX = x;
if (y < minY) minY = y;
if (x > maxX) maxX = x;
if (y > maxY) maxY = y;
}
}
}
}
}
console.timeEnd('flat bbox');
console.time('each-based bbox');
{
let minX = Infinity;
let minY = Infinity;
let maxX = Infinity;
let maxY = Infinity;
function eachFeature(feature) {
feature.forEach(eachPolygon);
}
function eachPolygon(polygon) {
polygon.forEach(eachRing);
}
function eachRing(ring) {
forEachPoint(ring, eachPoint);
}
function eachPoint(x, y) {
if (x < minX) minX = x;
if (y < minY) minY = y;
if (x > maxX) maxX = x;
if (y > maxY) maxY = y;
}
features.forEach(eachFeature);
}
console.timeEnd('each-based bbox');
populate storage: 2703.748ms
flat bbox: 275.649ms
each-based bbox: 258.523ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment