Transform .at to property access to improve performance (#14590)

Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
SUZUKI Sosukeandfisker Cheung authored and GitHub committed 2023-03-26 04:14:48 +09:00
1 parent 4edb68ac50
commit 00e59ed008
2 files changed
+9 -5

No files matched your search

+8 -3
View File
@@ -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;
+1 -2
View File
@@ -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} */