From b1bb5c53b8caca4cc52a4a94488d551319082797 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Wed, 27 Apr 2022 13:18:25 -0700 Subject: [PATCH] chore: Reduce number of calls to getAttribute (#6119) * Reduce number of calls to getAttribute ~1% speedup. * Improve comment. --- core/utils/svg_math.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/utils/svg_math.js b/core/utils/svg_math.js index d761f0c7b..57efc130a 100644 --- a/core/utils/svg_math.js +++ b/core/utils/svg_math.js @@ -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); }