noDelete
Este conteúdo não está disponível em sua língua ainda.
Diagnostic Category: lint/performance/noDelete
Since: v1.0.0
Disallow the use of the delete
operator.
The delete
operator enables the removal of a property from an object.
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.
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
Section titled ExamplesInvalid
Section titled Invalidcode-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 │
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 │