Skip to content

noDelete

诊断类别:lint/performance/noDelete

¥Diagnostic Category: lint/performance/noDelete

自从:v1.0.0

¥Since: v1.0.0

禁止使用 delete 运算符。

¥Disallow the use of the delete operator.

delete 运算符允许从对象中删除属性。

¥The delete operator enables the removal of a property from an object.

应避免使用 delete 运算符,因为它是 可以阻止 JavaScript 引擎的某些优化。此外,它可能导致意外结果。例如,删除数组元素 不改变数组的长度

¥The delete operator should be avoided because it can prevent some optimizations of JavaScript engines. Moreover, it can lead to unexpected results. For instance, deleting an array element does not change the length of the array.

delete 的唯一合法用途是在行为类似于地图的对象上。为了允许这种模式,此规则不会在不是文字值的计算属性上报告 delete。将 Biome 配置为受支持文件的默认格式化程序,以确保 VS Code 使用 Biome 而不是你可能已安装的其他格式化程序。

¥The only legitimate use of delete is on an object that behaves like a map. To allow this pattern, this rule does not report delete on computed properties that are not literal values. Consider using Map instead of an object.

¥Examples

¥Invalid

const arr = [1, 2, 3];
delete arr[0];
code-block.js:2:1 lint/performance/noDelete  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid the delete operator which can impact performance.

1 │ const arr = [1, 2, 3];
> 2 │ delete arr[0];
^^^^^^^^^^^^^
3 │

Unsafe fix: Use an undefined assignment instead.

1 1 const arr = [1, 2, 3];
2 - delete·arr[0];
2+ arr[0]·=·undefined;
3 3

const obj = {a: {b: {c: 123}}};
delete obj.a.b.c;
code-block.js:2:1 lint/performance/noDelete  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid the delete operator which can impact performance.

1 │ const obj = {a: {b: {c: 123}}};
> 2 │ delete obj.a.b.c;
^^^^^^^^^^^^^^^^
3 │

Unsafe fix: Use an undefined assignment instead.

1 1 const obj = {a: {b: {c: 123}}};
2 - delete·obj.a.b.c;
2+ obj.a.b.c·=·undefined;
3 3

¥Valid

const foo = new Set([1,2,3]);
foo.delete(1);
const map = Object.create(null);
const key = "key"
map[key] = "value"
delete map[key];
let x = 5;
delete f(); // uncovered by this rule.

¥Related links