Skip to content

Instantly share code, notes, and snippets.

@mourner
Created April 27, 2018 13:02
Show Gist options
  • Select an option

  • Save mourner/3c5de0e31ed8c8fc470de5b45f9fe482 to your computer and use it in GitHub Desktop.

Select an option

Save mourner/3c5de0e31ed8c8fc470de5b45f9fe482 to your computer and use it in GitHub Desktop.

Revisions

  1. mourner created this gist Apr 27, 2018.
    79 changes: 79 additions & 0 deletions bench_geodata.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@

    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;
    features.forEach((feature) => {
    feature.forEach((polygon) => {
    polygon.forEach((ring) => {
    forEachPoint(ring, (x, y) => {
    if (x < minX) minX = x;
    if (y < minY) minY = y;
    if (x > maxX) maxX = x;
    if (y > maxY) maxY = y;
    });
    });
    });
    });
    }
    console.timeEnd('each-based bbox');
    3 changes: 3 additions & 0 deletions result.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    populate storage: 2567.200ms
    flat bbox: 271.660ms
    each-based bbox: 467.415ms