Created
November 19, 2023 13:58
-
-
Save phillippelevidad/3ca74a2212cdd09d3bfa1e5132b56c21 to your computer and use it in GitHub Desktop.
Benchmarks for delete, Reflect.deleteProperty and the spread operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Benchmark from "benchmark"; | |
function useDelete() { | |
const obj = {}; | |
obj.a = 1; | |
obj.b = 2; | |
delete obj.b; | |
} | |
function useReflect() { | |
const obj = {}; | |
obj.a = 1; | |
obj.b = 2; | |
Reflect.deleteProperty(obj, "b"); | |
} | |
function useSpread() { | |
const obj = {}; | |
obj.a = 1; | |
obj.b = 2; | |
const { b, ...rest } = obj; | |
} | |
new Benchmark.Suite() | |
.add("delete", function () { | |
useDelete(); | |
}) | |
.add("reflect", function () { | |
useReflect(); | |
}) | |
.add("spread", function () { | |
useSpread(); | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}) | |
.run(); | |
// My results: | |
// delete x 19,286,059 ops/sec ±0.71% (91 runs sampled) | |
// reflect x 18,877,376 ops/sec ±0.72% (93 runs sampled) | |
// spread x 19,258,554 ops/sec ±0.93% (89 runs sampled) | |
// Fastest is delete,spread |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment