var removeId = 'd8808b4f-691a-407a-b6c9-8cc56af7749f'
var arr = [
"e56f7b57-b898-4bd7-a964-1f82464fc843",
"4628a5d8-2cb2-4891-8ec5-b86ad0b36b92",
"2b6f5c85-546d-456f-b424-bdcc02c6e1a5",
"ed1c66e4-6844-4568-a9f7-65046b4c1439",
"fc0ca740-dbf7-4cfe-936b-ee256371b1a1",
"b8a09387-291f-4de3-86ab-7440cf7251b5",
"92230a87-bf02-4f76-99f8-f5b2f282c902",
"9398caa6-125e-47ba-844d-041999617f2e",
"52347464-5393-44c5-a5bc-57d1e81dab38",
"d8808b4f-691a-407a-b6c9-8cc56af7749f",
"9b23e221-1dea-4bc6-bbc5-83cef4107209",
"a0f77397-3897-4a69-9f26-38cf801130c4",
"3c7cd54b-50e5-4838-bc88-fe9dcb0e5f57",
"299ca171-7116-42de-a172-6931fd49e003",
"e787469f-b138-40e0-a0a8-ef85443390e6",
"f698ee28-16a8-4413-a66f-c9c53db69e30",
"b9538174-224f-4b93-b9d3-9570b8bc3e2b",
"65ff3d6e-67f3-4de0-b526-dba1d58956de",
"83468296-e041-4a2a-9cab-3c51f8c532a3",
"1b2ee107-3abd-4876-a37b-d7f93e02c161",
"ec8bfd67-d4fe-4ec0-b896-b1e4a2fbb2a4",
"1b79d9ba-aac4-488d-bbff-8a3a0e6d6aea",
"f722cf01-0355-496f-a8aa-96384d4004d2",
"427cabda-59cb-4ec4-a3ec-ae6dcfe1381e",
"29442aa7-902c-4723-82c6-34070c843316",
"717de5f5-c69f-4ea5-bb21-b7ad8438c39a",
"ed537f1f-014e-4ac1-88b3-3620ee1f4e27",
"34a9bdfa-24de-4792-9755-200da425c802",
"ff30abab-8e24-49dd-8baa-87938190ee79",
"5c175ace-8911-4d58-a5dd-27c24a505554",
"e2d4cc87-2c38-48b1-89d6-6f5120324b10",
"5525bc2c-c08d-407b-ac58-72702c8fe1be",
"cfaff4a5-f0e8-4c68-b83d-fabf38f5caa7",
"3c9494d4-3b6b-4387-a362-c4e1e3611104",
"5e4a0899-53c8-45fc-96dc-ada70ae74d50",
"9a0e192d-1bdc-484b-b12c-31edb227c926",
"e41ad784-6f76-45b1-a33c-9003405d2166",
"7a9e7c03-7375-436c-a4a6-73fdead87a34"
]
const testSet = new Set(arr)
testSet.delete(removeId)
const answer = Array.from(testSet)
const testSet = new Set(arr)
testSet.delete(removeId)
const answer = [testSet]
const answer = arr.filter(id => id !== removeId)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set 1 | |
Set 2 | |
filter |
Test name | Executions per second |
---|---|
Set 1 | 161122.7 Ops/sec |
Set 2 | 411760.4 Ops/sec |
filter | 327082.0 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Definition
The benchmark consists of three individual test cases, each with its own script preparation code. The test cases are:
Set
data structure....
) on a Set
.filter()
method.What is being tested?
In each test case, the goal is to remove a specific ID (removeId
) from an array of IDs (arr
). The tests measure which approach is faster.
Options compared
The three approaches are:
Set
data structure to store unique values and then removes the specified ID....
) on a Set
to create a new array with all elements except the specified ID.filter()
method to create a new array with only the elements that do not match the specified ID.Pros and Cons of each approach
filter()
method, avoids the need to create an intermediate array.Set
or spread operator, as it creates a new array.Latest Benchmark Result
The latest results show that:
The results suggest that using a Set
data structure (either directly or via the spread operator) may be the most efficient way to remove an ID from an array in this specific benchmark.