var obj = {
"one": null,
"two": null,
}
delete obj["two"]
if (obj["two"] !== undefined) {
delete obj["two"]
}
delete obj["three"]
if (obj["two"] !== undefined) {
delete obj["three"]
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Delete Existing, No Check | |
Delete Existing, Check | |
Delete Nonexisting, No Check | |
Delete Nonexisting, Check |
Test name | Executions per second |
---|---|
Delete Existing, No Check | 11336077.0 Ops/sec |
Delete Existing, Check | 6281679.0 Ops/sec |
Delete Nonexisting, No Check | 10641756.0 Ops/sec |
Delete Nonexisting, Check | 6305477.5 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript benchmark test case on MeasureThat.net. The goal of this benchmark is to compare the performance of different approaches for deleting a property from an object in JavaScript.
Script Preparation Code
The script preparation code defines an object obj
with two properties, "one"
and "two"
, both initialized as null
.
var obj = {
"one": null,
"two": null
}
This setup is used as a common base for all test cases.
Test Cases
There are four individual test cases:
delete obj["two"]
This test case deletes the "two"
property from the obj
object without performing any existence checks.
if (obj["two"] !== undefined) {
delete obj["two"]
}
This test case first checks if the "two"
property exists in the obj
object and only then deletes it.
delete obj["three"]
This test case attempts to delete a non-existent property "three"
from the obj
object without performing any existence checks.
if (obj["two"] !== undefined) {
delete obj["three"]
}
This test case first checks if the "two"
property exists in the obj
object and only then attempts to delete a non-existent property "three"
.
Library: None
There are no external libraries used in these benchmark test cases.
Special JS Feature/Syntax: None
None of the provided test cases utilize any special JavaScript features or syntax, such as async/await, Promises, or modern ES6+ features.
Performance Comparison
The benchmark tests the performance of deleting a property from an object using two approaches:
The results show that direct deletion without existence checks is generally faster than performing an existence check.
Pros and Cons
Alternatives
Other approaches for deleting a property from an object in JavaScript include:
in
operator to check for property existence, e.g., if ("two" in obj) { delete obj["two"] }
.hasOwnProperty()
method on the prototype chain, e.g., if (obj.hasOwnProperty("two")) { delete obj["two"] }
.Object.prototype.hasOwnProperty.call()
to check for property existence, e.g., if (Object.prototype.hasOwnProperty.call(obj, "two")) { delete obj["two"] }
.However, these alternatives may not provide the same performance benefits as direct deletion without existence checks.