From 78d9ca01eb73cbc97bb264f52385dc8e3b8a4946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Tue, 4 Apr 2023 23:00:50 +0200 Subject: [PATCH 1/2] Remove unnecessary assignments and array copy --- src/polyline.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/polyline.js b/src/polyline.js index 70be433..cb131cb 100644 --- a/src/polyline.js +++ b/src/polyline.js @@ -48,9 +48,9 @@ polyline.decode = function(str, precision) { lat = 0, lng = 0, coordinates = [], - shift = 0, - result = 0, - byte = null, + shift, + result, + byte, latitude_change, longitude_change, factor = Math.pow(10, Number.isInteger(precision) ? precision : 5); @@ -60,8 +60,7 @@ polyline.decode = function(str, precision) { // loop iteration, a single coordinate is decoded. while (index < str.length) { - // Reset shift, result, and byte - byte = null; + // Reset shift and result shift = 1; result = 0; @@ -118,7 +117,7 @@ polyline.encode = function(coordinates, precision) { function flipped(coords) { var flipped = []; for (var i = 0; i < coords.length; i++) { - var coord = coords[i].slice(); + var coord = coords[i]; flipped.push([coord[1], coord[0]]); } return flipped; From 8f29a78fceb0edadc45c88bde662772e976f9693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Thu, 6 Apr 2023 01:05:46 +0200 Subject: [PATCH 2/2] Round each coordinate only once --- src/polyline.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/polyline.js b/src/polyline.js index cb131cb..5d260b8 100644 --- a/src/polyline.js +++ b/src/polyline.js @@ -13,12 +13,10 @@ var polyline = {}; function py2_round(value) { // Google's polyline algorithm uses the same rounding strategy as Python 2, which is different from JS for negative values - return Math.floor(Math.abs(value) + 0.5) * (value >= 0 ? 1 : -1); + return value < 0 ? Math.ceil(value - 0.5) : Math.round(value); } -function encode(current, previous, factor) { - current = py2_round(current * factor); - previous = py2_round(previous * factor); +function encode(current, previous) { var coordinate = (current - previous) * 2; if (coordinate < 0) { coordinate = -coordinate - 1 @@ -100,15 +98,18 @@ polyline.decode = function(str, precision) { * @returns {String} */ polyline.encode = function(coordinates, precision) { - if (!coordinates.length) { return ''; } - var factor = Math.pow(10, Number.isInteger(precision) ? precision : 5), - output = encode(coordinates[0][0], 0, factor) + encode(coordinates[0][1], 0, factor); - - for (var i = 1; i < coordinates.length; i++) { - var a = coordinates[i], b = coordinates[i - 1]; - output += encode(a[0], b[0], factor); - output += encode(a[1], b[1], factor); + output = '', + bLat = 0, + bLon = 0; + + for (var i = 0; i < coordinates.length; i++) { + var a = coordinates[i]; + var aLat = py2_round(a[0] * factor); + var aLon = py2_round(a[1] * factor); + output += encode(aLat, bLat) + encode(aLon, bLon); + bLat = aLat; + bLon = aLon; } return output;