perf: faster getDistance calculations #2684
Open
+1
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR brings to the table some performance improvement for
getDistance
calculations. The performance improvement is more evident in Chrome, whereas in Safari the ops/s gains look minimal.Here's a quick benchmark: https://jsbenchmark.com/#eyJjYXNlcyI6W3siaWQiOiJ3N25aUzVrc2hScHBzSlRoTldIMGgiLCJjb2RlIjoiREFUQS5mb3JFYWNoKCh7IHgyLCB4MSwgeTIsIHkxLCB9KSA9PiAgTWF0aC5zcXJ0KCgoeDIgLSB4MSkgKiogMikgKyAoKHkyIC0geTEpICoqIDIpKSkiLCJuYW1lIjoiSW5saW5lIiwiZGVwZW5kZW5jaWVzIjpbXX0seyJpZCI6IlpaSjZycGo0NFhSOFdHZVdraXMtdiIsImNvZGUiOiJEQVRBLmZvckVhY2goKHsgeDIsIHgxLCB5MiwgeTEsIH0pID0-IHtcbiAgICBsZXQgZGVsdGFYID0geDIgLSB4MVxuICAgIGxldCBkZWx0YVkgPSB5MiAtIHkxXG5cbiAgICBkZWx0YVggKj0gZGVsdGFYXG4gICAgZGVsdGFZICo9IGRlbHRhWVxuXG4gICAgcmV0dXJuIE1hdGguc3FydChkZWx0YVggKyBkZWx0YVkpXG59KSIsIm5hbWUiOiJMZXQiLCJkZXBlbmRlbmNpZXMiOltdLCJhc3luYyI6ZmFsc2V9XSwiY29uZmlnIjp7Im5hbWUiOiJOaXZvIGdldERpc3RhbmNlIiwicGFyYWxsZWwiOnRydWUsImdsb2JhbFRlc3RDb25maWciOnsiZGVwZW5kZW5jaWVzIjpbXX0sImRhdGFDb2RlIjoicmV0dXJuIEFycmF5LmZyb20oeyBsZW5ndGg6IDFfMDAwIH0sIChfLCBpKSA9PiAoe1xuICB4MjogaSsxLFxuICB4MTogaSsyLFxuICB5MjogaSszLFxuICB5MTogaSs0XG59KSkifX0
While, as I said, the perf increase is neglibile in Safari, the numbers look nice in Chrome where I measured a 204% increase in ops/s, as shown in the screenshow below:

Even if this translates in marginal QOL enhancements in real life use-cases, it's still a free win.