Transform .at to property access to improve performance (#14590)
Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
1 parent
4edb68ac50
commit
00e59ed008
2 files changed
+9
-5
No files matched your search
@@ -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;
|
||||
@@ -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} */
|
||||
|
||||
Reference in new issue
Block a user