// square distance from a point to a segment
function getSqSegDist(p, p1, p2) {
    var x = p1[0],
        y = p1[1],
        dx = p2[0] - x,
        dy = p2[1] - y;

    if (dx !== 0 || dy !== 0) {

        var t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy);

        if (t > 1) {
            x = p2[0];
            y = p2[1];

        } else if (t > 0) {
            x += dx * t;
            y += dy * t;
        }
    }

    dx = p[0] - x;
    dy = p[1] - y;

    return dx * dx + dy * dy;
}

// simplification using optimized Douglas-Peucker algorithm with recursion elimination
module.exports = function simplifyDouglasPeucker(points, tolerance) {
    if (points.length<=1)
        return points;
    tolerance = typeof tolerance === 'number' ? tolerance : 1;
    var sqTolerance = tolerance * tolerance;

    var len = points.length,
        MarkerArray = typeof Uint8Array !== 'undefined' ? Uint8Array : Array,
        markers = new MarkerArray(len),
        first = 0,
        last = len - 1,
        stack = [],
        newPoints = [],
        i, maxSqDist, sqDist, index;

    markers[first] = markers[last] = 1;

    while (last) {

        maxSqDist = 0;

        for (i = first + 1; i < last; i++) {
            sqDist = getSqSegDist(points[i], points[first], points[last]);

            if (sqDist > maxSqDist) {
                index = i;
                maxSqDist = sqDist;
            }
        }

        if (maxSqDist > sqTolerance) {
            markers[index] = 1;
            stack.push(first, index, index, last);
        }

        last = stack.pop();
        first = stack.pop();
    }

    for (i = 0; i < len; i++) {
        if (markers[i]) newPoints.push(points[i]);
    }

    return newPoints;
}
@@ -508,693 +508,142 @@
 %0A%7D%0A%0A
-// simplification using optimized Douglas-Peucker algorithm with recursion elimination%0Amodule.exports = function simplifyDouglasPeucker(points, tolerance) %7B%0A    if (points.length%3C=1)%0A        return points;%0A    tolerance = typeof tolerance === 'number' ? tolerance : 1;%0A    var sqTolerance = tolerance * tolerance;%0A%0A    var len = points.length,%0A        MarkerArray = typeof Uint8Array !== 'unde
+function simplifyDPStep(points, first, last, sqTolerance, simpli
 fi
-n
 ed
-' ? Uint8Array : Array,%0A        markers = new MarkerArray(len),%0A        first = 0,%0A        last = len - 1,%0A        stack = %5B%5D,%0A        newPoints = %5B%5D,%0A        i, maxSqDist, sqDist, index;%0A%0A    markers%5Bfirst%5D = markers%5Blast%5D = 1;%0A%0A    while (last) %7B%0A%0A        maxSqDist = 0;%0A%0A        fo
+) %7B%0A    var maxSqDist = sqTolerance,%0A        index;%0A%0A    for (va
 r 
-(
 i = 
@@ -678,19 +678,19 @@
         
-   
+var
  sqDist 
@@ -750,28 +750,24 @@
 );%0A%0A        
-    
 if (sqDist %3E
@@ -788,28 +788,24 @@
             
-   
- 
 index = i;%0A 
@@ -803,20 +803,16 @@
 ex = i;%0A
-    
         
@@ -835,20 +835,16 @@
 sqDist;%0A
-    
         
@@ -845,35 +845,27 @@
       %7D%0A    
-    %7D%0A%0A    
+%7D%0A%0A
     if (maxS
@@ -899,223 +899,668 @@
     
-    markers%5Bindex%5D = 1;%0A            stack.push(first, index, index, last);%0A        %7D%0A%0A        last = stack.pop();%0A        first = stack.pop();%0A    %7D%0A%0A    for (i = 0; i %3C len; i++) %7B%0A        if (markers%5Bi%5D) newPoints
+if (index - first %3E 1) simplifyDPStep(points, first, index, sqTolerance, simplified);%0A        simplified.push(points%5Bindex%5D);%0A        if (last - index %3E 1) simplifyDPStep(points, index, last, sqTolerance, simplified);%0A    %7D%0A%7D%0A%0A// simplification using Ramer-Douglas-Peucker algorithm%0Amodule.exports = function simplifyDouglasPeucker(points, tolerance) %7B%0A    if (points.length%3C=1)%0A        return points;%0A    tolerance = typeof tolerance === 'number' ? tolerance : 1;%0A    var sqTolerance = tolerance * tolerance;%0A    %0A    var last = points.length - 1;%0A%0A    var simplified = %5Bpoints%5B0%5D%5D;%0A    simplifyDPStep(points, 0, last, sqTolerance, simplified);%0A    simplified
 .pus
@@ -1572,18 +1572,15 @@
 nts%5B
-i%5D);%0A    %7D
+last%5D);
 %0A%0A  
@@ -1592,16 +1592,18 @@
 urn 
-newPoints
+simplified
 ;%0A%7D
+%0A

use recursive implementation since it is much faster