Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
deleting multiple values from array
(version: 4)
Comparing performance of:
splice vs delete + Object.values vs filter
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var len = 100000; var arr = Array(len).fill(42); var deleteIndexes = Array.from(Array(1000), () => (Math.random() * len)|0); console.log("deleteIndexes", deleteIndexes);
Tests:
splice
for (const i of deleteIndexes) { arr.splice(i, 1); }
delete + Object.values
for (const i of deleteIndexes) { delete arr[i]; } let res = Object.values(arr);
filter
arr.filter((val, idx) => deleteIndexes.includes(idx))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
splice
delete + Object.values
filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared options, pros and cons of each approach, library usage, special JS features or syntax, and alternative approaches. **Benchmark Overview** The benchmark tests three different approaches to deleting multiple values from an array in JavaScript: 1. Using `splice()` 2. Using `delete` operator followed by `Object.values()` to create a new array 3. Using `Array.prototype.filter()` with the `includes()` method Each test case has a unique name, and their respective benchmark definition scripts are provided. **Options Compared** The three options being compared are: 1. **splice()**: A built-in JavaScript method that modifies the original array by removing elements at specified indices. 2. **delete + Object.values()**: Using the `delete` operator to remove individual elements from the array, and then using `Object.values()` to create a new array with the remaining elements. 3. **filter() with includes()**: Using the `includes()` method to check if an index exists in the `deleteIndexes` array before including it in the filtered array. **Pros and Cons of Each Approach** 1. **splice()**: * Pros: Efficient, fast, and straightforward. * Cons: Modifies the original array, which may be undesirable depending on the use case. 2. **delete + Object.values()**: * Pros: Creates a new array with remaining elements, preserving the original array's integrity. * Cons: May be slower due to the overhead of creating a new array using `Object.values()` and potentially slower delete operations. 3. **filter() with includes()**: * Pros: Preserves the original array's integrity like `delete + Object.values()`, but with a simpler syntax. * Cons: May be slower than `splice()` due to the overhead of calling `includes()` for each index. **Library Usage** None of the benchmarked scripts use any external libraries. **Special JS Features or Syntax** The benchmark uses two special features: 1. **Template literals**: Used in the `Script Preparation Code` and `Individual test cases` sections. 2. **Array.from()**: Used to create an array from an iterable (in this case, another array). Note that these are not critical features, as they are part of the JavaScript language. **Alternative Approaches** Some alternative approaches to deleting multiple values from an array include: 1. Using `map()` and slicing: Create a new array with only the desired elements using `map()`, and then slice it to remove unnecessary elements. 2. Using `reduce()` and filtering: Use `reduce()` to iterate over the original array, applying a filter function to each element. 3. Using `forEach()` and splicing: Use `forEach()` to iterate over the original array, modifying it in place using `splice()`. 4. Using `set`: If you're using modern JavaScript (ES6+), you can use `Set` data structure to remove duplicates from an array. Each of these approaches has its own trade-offs and may be more or less suitable depending on your specific use case.
Related benchmarks:
splice vs delete on different types random index
remove by splice vs filter array v5
Remove element from array
Remove element from array v2
Comments
Confirm delete:
Do you really want to delete benchmark?