From 00e59ed008a8e6f5e23d5ba61dee0a302df7fbb1 Mon Sep 17 00:00:00 2001 From: SUZUKI Sosuke Date: Sun, 26 Mar 2023 04:14:48 +0900 Subject: [PATCH] Transform `.at` to property access to improve performance (#14590) Co-authored-by: fisker Cheung --- scripts/build/shims/at.js | 11 ++++++++--- src/common/ast-path.js | 3 +-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/build/shims/at.js b/scripts/build/shims/at.js index adc7cd756..799238faf 100644 --- a/scripts/build/shims/at.js +++ b/scripts/build/shims/at.js @@ -10,9 +10,14 @@ const at = (isOptionalObject, object, index) => { return; } - return object.at - ? object.at(index) - : object[index < 0 ? object.length + index : index]; + // "relative indexing" is not available in Node.js v14 + // `.at` method is slower than property access on Node.js v16 and v18, see #14396 + // Only checked arrays and strings, since we haven't use `TypedArray`, and the transform only works for sources + if (Array.isArray(object) || typeof object === "string") { + return object[index < 0 ? object.length + index : index]; + } + + return object.at(index); }; export default at; diff --git a/src/common/ast-path.js b/src/common/ast-path.js index f1b86697b..41f379d1a 100644 --- a/src/common/ast-path.js +++ b/src/common/ast-path.js @@ -16,8 +16,7 @@ class AstPath { /** @type {object} */ get node() { - // eslint-disable-next-line unicorn/prefer-at -- `Array#at` is slow on Node.js v16 and v18 - return this.stack[this.stack.length - 1]; + return this.stack.at(-1); } /** @type {object | null} */