chore: Reduce number of calls to getAttribute (#6119)

* Reduce number of calls to getAttribute

~1% speedup.

* Improve comment.
This commit is contained in:
Neil Fraser
2022-04-27 13:18:25 -07:00
committed by GitHub
parent 45c36f8982
commit b1bb5c53b8

View File

@@ -56,11 +56,14 @@ const XY_STYLE_REGEX =
const getRelativeXY = function(element) {
const xy = new Coordinate(0, 0);
// First, check for x and y attributes.
const x = element.getAttribute('x');
// Checking for the existence of x/y properties is faster than getAttribute.
// However, x/y contains an SVGAnimatedLength object, so rely on getAttribute
// to get the number.
const x = element.x && element.getAttribute('x');
const y = element.y && element.getAttribute('y');
if (x) {
xy.x = parseInt(x, 10);
}
const y = element.getAttribute('y');
if (y) {
xy.y = parseInt(y, 10);
}