Skip to content

Commit

Permalink
Optimize: ~30x speedup in the benchmark.
Browse files Browse the repository at this point in the history
turf-explode x 101,825 ops/sec ±0.83% (95 runs sampled)
turf-explode x 3,006,464 ops/sec ±2.35% (91 runs sampled)
  • Loading branch information
tmcw committed Dec 30, 2014
1 parent ea53333 commit fe1e25e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 85 deletions.
8 changes: 4 additions & 4 deletions bench.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var explode = require('./');
global.explode = require('./');
var Benchmark = require('benchmark');
var fs = require('fs');
var polygon = require('turf-polygon');
var point = require('turf-point');
var featurecollection = require('turf-featurecollection');

var poly = polygon([[[0,0], [0,10], [10,10] , [10,0]]]);
global.poly = polygon([[[0,0], [0,10], [10,10] , [10,0]]]);
var p1 = point(0,0),
p2 = point(0,10),
p3 = point(10,10),
Expand All @@ -15,12 +15,12 @@ var fc = featurecollection([p1,p2,p3,p4]);
var suite = new Benchmark.Suite('turf-explode');
suite
.add('turf-explode',function () {
explode(poly);
global.explode(global.poly);
})
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {

})
.run();
.run();
128 changes: 47 additions & 81 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,64 @@ var flatten = require('flatten');
var featureCollection = require('turf-featurecollection');
var point = require('turf-point');

module.exports = function(fc){
if(fc.type === 'FeatureCollection'){
for(var i in fc.features){
var coordinates ;
switch(fc.features[i].geometry.type){
case 'Point':
coordinates = [fc.features[i].geometry.coordinates];
break
case 'LineString':
coordinates = fc.features[i].geometry.coordinates;
break
case 'Polygon':
coordinates = fc.features[i].geometry.coordinates;
coordinates = flatCoords(coordinates);
break
case 'MultiPoint':
coordinates = fc.features[i].geometry.coordinates;
break
case 'MultiLineString':
coordinates = fc.features[i].geometry.coordinates;
coordinates = flatCoords(coordinates);
break
case 'MultiPolygon':
coordinates = fc.features[i].geometry.coordinates;
coordinates = flatCoords(coordinates);
break
}
if(!fc.features[i].geometry && fc.features[i].properties){
return new Error('Unknown Geometry Type');
}
}

var exploded = featureCollection([]);
module.exports = function(layer){

coordinates.forEach(function(coords){
exploded.features.push(point(coords[0], coords[1]));
})
var points = [];
if (layer.type === 'FeatureCollection') features = layer.features;
else if (layer.type === 'Feature') features = [layer];
else features = [{ geometry: layer }];

return exploded;
}
else{
var coordinates ;
var geometry;
if(fc.type === 'Feature'){
geometry = fc.geometry;
}
else{
geometry = fc;
}
switch(geometry.type){
for(var i = 0; i < features.length; i++){
var coords = features[i].geometry.coordinates;
switch(features[i].geometry.type){
case 'Point':
coordinates = [geometry.coordinates];
break
depth0(coords, points);
break;
case 'LineString':
coordinates = geometry.coordinates;
break
case 'Polygon':
coordinates = geometry.coordinates;
coordinates = flatCoords(coordinates);
break
case 'MultiPoint':
coordinates = geometry.coordinates;
break
depth1(coords, points);
break;
case 'Polygon':
case 'MultiLineString':
coordinates = geometry.coordinates;
coordinates = flatCoords(coordinates);
break
depth2(coords, points);
break;
case 'MultiPolygon':
coordinates = geometry.coordinates;
coordinates = flatCoords(coordinates);
break
}
if(!geometry){
return new Error('No Geometry Found');
depth3(coords, points);
break;
default:
return new Error('Unknown Geometry Type');
}
}
return featureCollection(points);
};

var exploded = featureCollection([]);
function depth0(coord, features) {
features.push(point(coord[0], coord[1]));
}

coordinates.forEach(function(coords){
exploded.features.push(point(coords[0], coords[1]));
})
function depth1(coords, features) {
for(var i = 0; i < coords.length; i++){
var coord = coords[i];
features.push(point(coord[0], coord[1]));
}
}

return exploded;
function depth2(coords, features) {
for(var i = 0; i < coords.length; i++){
for(var j = 0; j < coords[i].length; j++){
var coord = coords[i][j];
features.push(point(coord[0], coord[1]));
}
}
}

function flatCoords(coords){
var newCoords = [];
coords = flatten(coords);
coords.forEach(function(c, i){
if(i % 2 == 0) // if is even
newCoords.push([c, coords[i+1]]);
})
return newCoords;
}
function depth3(coords, features) {
for(var i = 0; i < coords.length; i++){
for(var j = 0; j < coords[i].length; j++){
for(var k = 0; k < coords[i][j].length; k++){
var coord = coords[i][j][k];
features.push(point(coord[0], coord[1]));
}
}
}
}

0 comments on commit fe1e25e

Please sign in to comment.